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 is outputed from Azure Form Recognizer, and i am trying to do a Liquid json 2 json in Logic Apps.
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!
According to the sample of json data and the liquid template you provided, I know there are more than one object under the property keyValuePairs
in your json data. So I think using "Parse JSON" action is not a good solution for it, we'd better use liquid template as you mentioned in your question. But I'm confused about that if there are more than one object under the property pages
in your json data ?
Now I assume the json data just has one object under the property pages
and provide the solution of liquid template for your reference (If the json data has more than one object under the property pages
, please provide some more details of your requirements and I will modify my solution).
I assume your json data like below:
{
"status": "success",
"pages": [
{
"number": 1,
"keyValuePairs": [
{
"key": [
{
"text": "Page No:",
"boundingBox": [
507.4
]
}
],
"value": [
{
"text": "1",
"boundingBox": [
586.8
],
"confidence": 1.0
}
]
},
{
"key": [
{
"text": "Page No:",
"boundingBox": [
507.4
]
}
],
"value": [
{
"text": "2",
"boundingBox": [
586.8
],
"confidence": 2.0
}
]
}
]
}
]
}
1. We need to get the object under the property pages
and put it in the "content" of "Transform JSON to JSON" action. We can parse the whole json data(with "Parse JSON" action) first and do something like below:
2. Upload the liquid template to integration account, please refer to my liquid template:
{
"results":[
{% for kvp in content.keyValuePairs %}
{%- if forloop.Last == true -%}
{
{% for mykey in kvp.key %}
"thiskey": "{{mykey.text}}",
{%- endfor -%}
{% for myval in kvp.value %}
"thisvalue": "{{myval.text}}",
"thisconfidence": "{{myval.confidence}}"
{%- endfor -%}
}
{%- else -%}
{
{% for mykey in kvp.key %}
"thiskey": "{{mykey.text}}",
{%- endfor -%}
{% for myval in kvp.value %}
"thisvalue": "{{myval.text}}",
"thisconfidence": "{{myval.confidence}}"
{%- endfor -%}
},
{%- endif -%}
{%- endfor -%}
]
}
3. After running this logic, we can get the result as:
{
"results": [
{
"thiskey": "Page No:",
"thisvalue": "1",
"thisconfidence": "1"
},
{
"thiskey": "Page No:",
"thisvalue": "2",
"thisconfidence": "2"
}
]
}
Something you need to be aware of:
Since we put the object under the property pages
to the content of "Transform JSON to JSON" action, so the content
in the liquid template represents it.
In liquid template, we need to be careful about the comma character, so I use {%- if forloop.Last == true -%}
to judge if it is the last object in the loop, and then judge if we need to add the comma character.
For your second question about what's the best approach for debugging Liquid ?
, unfortunately I think we can just complete the liquid template and upload to the integration account, then we can see the result. I don't know if there is a way for us to debug it easily.
Hope it helps~