Home > Mobile >  How to get value from associative json in shopify liquid
How to get value from associative json in shopify liquid

Time:06-26

{"collections"=>{"1656046380"=>[{"collection_title"=>"sport"}, {"question"=>"q_0", "answer"=>"a_0"}, {"question"=>"q_1", "answer"=>"a_1"}, {"question"=>"q_2", "answer"=>"a_2"}], "1656047118"=>[{"collection_title"=>"cars"}, {"question"=>"q_0", "answer"=>"a_0"}, {"question"=>"q_1", "answer"=>"a_1"}, {"question"=>"q_2", "answer"=>"a_2"}]}, "group"=>{"1656046878"=>[{"group_title"=>"page1"}, {"style"=>"3"}, {"selected_coll"=>"[\"1656046380\",\"1656046423\"]"}]}}

If this is my data which I am getting using liquid for loop

{%- for field in shop.metafields.advance_faq_app -%}
      <li>{{ shop.metafields.advance_faq_app.advance_faq_app }}</li><br/>
          
{%- endfor -%} 

I am able to get this data

"group"=>{"1656046878"=>[{"group_title"=>"page1"}, {"style"=>"3"}, {"selected_coll"=>"[\"1656046380\",\"1656046423\"]"}]}}

Using

{{ shop.metafields.advance_faq_app.advance_faq_app }} in the above for loop

How can I get the value of "group_title"

CodePudding user response:

Render your metafield data to a Javascript variable, and then you have access to all the keys and values to do as you wish. It is JSON data. So it makes sense to use JS.

CodePudding user response:

I'm not sure I undestand excatly the structure of your variables. But when looping inside a json liquid gives you the key in the [0] and the value in the [1].

So maybe you should be doing something like.

{%- for field in shop.metafields.advance_faq_app -%}
      {% for element in field %}
          {% if element[0] == 'group_title' %}
           ... 
          {% endif %}
      {% endfor %}
          
{%- endfor -%} 
  • Related