How do I loop through a JSON array of objects from a metaObject

Giltee
Shopify Partner
17 3 6

This seems like it should be pretty straight forward but I am not able to get this simple feature working.

 

I a meta Object with a json field that is an array of objects:

 

[{"id":"test","title":"my product"}]

When I try to loop through this array of objects in liquid only the top level keys are being supplied:

 

 

 

{% for item in shop.metaobjects.app--2315872--product_related_metaobject.values %}
   {{ item.productIds | json_parse }} // output: [{"id":"test","title":"my product"}]
       {% for product in item.productIds %} // try looping through the data
           {{product[0]}} // no output?
           {{product}} // no output???
       {% endfor %}
{% endfor %}

 

 

 

How can I loop through the JSON array and access the object keys / values?

 

 

 

 

Reply 1 (1)

iffikhan30
Shopify Partner
237 35 43

Hello @Giltee ,

 

Here is your solution,

 

{% for item in shop.metaobjects.app--2315872--product_related_metaobject.values %}
   {{ item.productIds | json_parse }} // output: [{"id":"test","title":"my product"}]
       {% for product in item.productIds %} // try looping through the data
           {{product.id}}
           {{product.title}}
       {% endfor %}
{% endfor %}
Custom theme and app [remix] expert.
Buy me a Coffee
Email: irfan.sarwar.khan30@gmail.com
Chat on WhatsApp