help trying to parse array in liquid

New Member
1 0 0

Hi there!

I am a complete newcomer to Liquid, started playing with it a few hours ago.

i am trying to parse a JSON and extract certain key value pairs out of it, yet i am not successful at all.

the JSON looks like this

{
  "status": "success",
  "pages": [
    {
      "number": 1,
      "keyValuePairs": [
        {
          "key": [
            {
              "text": "Page No:",
              "boundingBox": [
                507.4,
               ]
            }
          ],
          "value": [
            {
              "text": "1",
              "boundingBox": [
                586.8,
              ],
              "confidence": 1.0
            }
          ]
        },
....
...

what i am looking for is to loop through the keyValuePairs array, and then pull the "text" element from the "key" array, and the "text"+"confidence" element from the "value"array.

 

Could you please help me in understanding the approach here? I tried something like this, but the array comes back empty

{
"results":[
{% for kvp in content.keyValuePairs  %}

{% for mykey in kvp.key  %}

"thiskey":"{{mykey.text}}
{%- endfor -%}
{% for myval in kvp.value  %}

"thisvalue":{{myval.text}}
"thisconfidence":{{myval.confidence}}
{%- endfor -%}
{%- endfor -%}
]
}


And second question: what's the best approach for debugging Liquid, so i can see where i am getting it wrong and adjust?
 
Thank you!
0 Likes
Highlighted

Hi there. First of all, you should check the keyValuePairs in for loop to make sure that you can get data here.You can read and check in this link to get more about filter in liquid : https://shopify.dev/docs/liquid/reference/filters/array-filters 

Or you can try to parse Json here for Shopify Liquid if It's necessary: https://github.com/culturekings/shopify-json-parser

Other issue is that in result array you have an unused quotation marks(") here.
Screen Shot 2020-02-17 at 16.44.58.png
About second question, To debug liquid code, I think that you should use manual debugging of Code Editor that you use to code example: WebStorm or VSCode

- [Tutorial] How to customize Shopify product page: academy.pagefly.io/customize-shopify-product-pages
- Are you having problems customizing your Shopify pages? Check out PageFly - Advanced Shopify Page Builder
0 Likes