'How can I parse a json string value of a json object in a liquid template?
I am rather new to Liquid templates, but I don't seem to find a way to parse a string value to json.
Disclaimer: I am using the Shopify Liquid Preview extension for VSCode.
Input json file:
The input file contains two properties: Status and Body and I want to find a way to access the ItemData.Name json field inside the Body property.
{
"Status": true,
"Body": "{\"ItemData\":{\"Amount\":10.0,\"Name\":\"MyTest\",\"ItemType\":1}}"
}
Expected output:
<h1>MyTest</h1>
<p>Status: true<p>
Current attempt:
Trying to use the json_string and json filter, but that does not seem to work, at first sight.
{% assign data = Body | json_string %}
{% assign data2 = Body | json %}
<h1>{{data.ItemData.Name}}</h1>
<h1>{{data2.ItemData.Name}}</h1>
<p>{{Status}}</p>
Solution 1:[1]
I did this to successfully parse a JSON string in a project of mine:
{% assign json_str = cart.attributes.urban-deli-pickup-spot %}
{% assign json_token = json_str | split: ',' %}
{% for token in json_token %}
{% if forloop.index == 1 %}
{% assign res = token | split: ':' %}
{% assign resWithoutQuotes = res[1] | remove: '"' %}
{% capture pickup_spot %}
Your amazing truck pickup spot is: {{ resWithoutQuotes }}
{% endcapture %}
{% endif %}
{% endfor %}
{{ pickup_spot }}
Starting point ->
{"postal_address":"Evenemangsgatan 31","requested_delivery_date":"2022-02-03","timewindow_start":"13:10","timewindow_end":"14:00"}
Result ->
Your amazing truck pickup spot is: Evenemangsgatan 31
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dawid Dahl |
