Creating an object with JSON/Liquid - possible?

I'm trying to figure out if there is a way to create an object with liquid.

 

It would be easiest to show it as JSON,

data below are just placeholders, but my point is to be able to use liquid variables as values

"object": [
    {
      "name": "email",
      "value": "{{ customer.email }}"
    },
    {
      "name": "dummy_name",
      "value": "lalala"
    },
    {
      "name": "example",
      "value": "{{ shop.url }}"
    }]
  }

What I would like to do is to retrieve those values in forloop like this:

{% for o in object %}
  {{ o.name }}={{ o.id }}
{% endfor %}

Is this even possible?

 

 

Shopify Partner @ Tale Commerce
Selling on Shopify in Poland? Join our Shopify Polska group on Facebook!
KarlOffenberger
Shopify Partner
1868 183 818

If you store that JSON in a metafield value of type json_string then yes, you can. Otherwise, no you can't unless let there be dragons (please don't, just don't be even tempted).

Liked this post? You might also like our fantastic upsell apps Candy Rack, Last Upsell post-purchase and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie

Could I store liquid variable as json_string and expect it to be outputted according to its context on storefront?

Shopify Partner @ Tale Commerce
Selling on Shopify in Poland? Join our Shopify Polska group on Facebook!
0 Likes
KarlOffenberger
Shopify Partner
1868 183 818

Nope, not that I am aware of.

Liked this post? You might also like our fantastic upsell apps Candy Rack, Last Upsell post-purchase and Candy Cart or offer free gifts with Gift Box. All made with ❤️  and care by Digismoothie
0 Likes
Mircea_Piturca
Shopify Partner
1489 42 316

Let's hope we will see something like this in Liquid!

Probably this mean a lot of pain for the Shopify engineering cru but really helpful for devs and the language.

Liquid is great, Objects, new Array methods, working with data in general would be so nice.

 

Is there a feature request board? @Klarna

Thanks

Finally—Add variant descriptions to your products
TomTranzistor
Shopify Partner
31 2 5

Hey there, 

You can create a Js variable as JSON, here how to : 

<script>
var object = {}
{% for variant in product.variants %}
  object['{{ variant.id }}'] = "{{ variant.title }}"
{% endfor %}
console.log(object)
</script>

 

The way to do only in Liquid, is to create arrays, et match values by index.

 

Let us help you - Tranzistor.tech
0 Likes