Get key and value data from json with Liquid

340 views Asked by At

I have a json data. I need key and value data from it. My json looks like this.

{
    "content": {
        "key1": "value1",
        "key2": "value2",        
    }
}

Need to convert to the below format.

{ 
    "Key": "key1",
    "Value": "value1"
}

I tried below liquid. I get data as [key1,value1].

  {%- for data in content -%}
    "Key": {{data[0]}}  
    "Value": {{data[1]}}
  {%- endfor -%}
1

There are 1 answers

3
Md Rezowan Hossain Suvro On

In Liquid, you can extract key value pairs from a JSON object like the one you provided. But, you can access the keys and values in the JSON object in the following way:

{% assign content = {
    "key1": "value1",
    "key2": "value2"
} %}

{% for key in content %}
  "Key": "{{ key }}",
  "Value": "{{ content[key] }}"
{% endfor %}