How to use JSON data in Shopify liquid code.
2 min readDec 17, 2020
Originally Posted at https://iliashaddad.com/blog/how-to-use-JSON-with-Shopify-liquid
In the 11th article of the #4weeksOfShopifydev challenge, I’ll be talking about how to use JSON data in Shopify liquid code.
In Shopify Liquid template language, they don’t support JSON or objects (not product objects), I have a tip to use it in Liquid.
- First, we need to install Metafields Guru app to create metafields
- Create new meta field under Shop settings
- Select JSON string as type and type data as key
- Use this JSON sample data
[ { "firstName": "Ilias", "lastName": "Haddad", "email": "contact@iliashaddad.com" }]
- To consume this JSON data in Liquid, you can use this code snippet to render this JSON data
{% assign users = shop.metafields.global %}{% for user in users.data %}{{ user.firstName}} {{ user.lastName}} {{ user.email}}{% endfor %}
In this sinppet, we assign the global namespace metafield to users variable
{% assign users = shop.metafields.global %}
And we loop over the array of object in JSON by accessing the data key metafield