Metafield json_string won't iterate or display properly

Highlighted
Excursionist
14 3 2

I have a metafield of type json_string called "lessons" that looks like the following:

 

 

{
"key" : "value",
"key" : "value",
"key" : "value"
}

 

 

I store it as a string which looks like this:

 

 

"{\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"}

 

 

 Following the documentation I cannot get this to display in my theme correctly with the following liquid:

 

 

 <ul>
    {% assign lessons = product.metafields.global['lessons'] %}
    {% for key_value in lessons %}
      <li>{{ key_value[0] }}: {{ key_value[1] }}</li>
    {% endfor %}
  </ul>

 

 

 The output is simply a semicolon.

However, if I place the "lessons" string into a <p>, the json_string will show up. What am I doing wrong?

0 Likes
Highlighted
Excursionist
14 3 2

I figured out the problem. I was storing the data in the metafield as a JSON string. What it really wants is raw JSON. Not sure why it's called a json_string.

0 Likes