我是Liquid的新手,几小时前就开始玩它。
我正在尝试解析JSON并从中提取某些键值对,但我一点都不成功。 JSON是从Azure Form Recognizer输出的,我正在尝试在Logic Apps中执行Liquid json 2 json。
JSON看起来像这样
{
"status": "success",
"pages": [
{
"number": 1,
"keyValuePairs": [
{
"key": [
{
"text": "Page No:",
"boundingBox": [
507.4
]
}
],
"value": [
{
"text": "1",
"boundingBox": [
586.8
],
"confidence": 1.0
}
]
},
....
...
我要寻找的是循环遍历keyValuePairs数组,然后从“键”数组中拉出“文本”元素,并从“值”数组中拉出“文本” +“信心”元素。
能否请您帮助我了解此处的方法?我尝试过类似的操作,但是数组返回为空
{
"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 -%}
]
}
第二个问题:调试Liquid的最佳方法是什么,这样我就可以看到我在哪里弄错了并进行调整?
谢谢!
答案 0 :(得分:1)
根据json数据的样本和您提供的液体模板,我知道json数据中的属性keyValuePairs
下有多个对象。因此,我认为使用“ Parse JSON”操作不是一个好的解决方案,我们最好使用您在问题中提到的液体模板。但是我感到困惑的是,如果您的json数据的属性pages
下有多个对象?
现在,我假设json数据在属性pages
下只有一个对象,并提供了液体模板的解决方案供您参考(如果json数据在属性pages
下有多个对象,请提供您要求的更多详细信息,我将修改我的解决方案。
我假设您的json数据如下:
{
"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。。我们需要将对象放在属性pages
下,并将其放在“将JSON转换为JSON”操作的“内容”中。我们可以先解析整个json数据(使用“解析JSON”操作),然后执行以下操作:
2。。将临时模板上传到集成帐户,请参阅我的临时模板:
{
"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。。运行此逻辑后,我们可以得到以下结果:
{
"results": [
{
"thiskey": "Page No:",
"thisvalue": "1",
"thisconfidence": "1"
},
{
"thiskey": "Page No:",
"thisvalue": "2",
"thisconfidence": "2"
}
]
}
您需要注意的事项:
由于我们将对象放在属性pages
下的“将JSON转换为JSON”操作的内容中,因此液体模板中的content
代表了它。
在液体模板中,我们需要注意逗号字符,因此我使用{%- if forloop.Last == true -%}
来判断它是否是循环中的最后一个对象,然后判断是否需要添加逗号字符。
对于您关于what's the best approach for debugging Liquid ?
的第二个问题,很遗憾,我认为我们只需完成流动模板并上传到集成帐户,即可看到结果。我不知道我们是否可以轻松地对其进行调试。
希望有帮助〜
答案 1 :(得分:0)
我已经尝试使用示例JSON有效负载来设计工作流程。
我已经解析了JSON模式并从中提取了所需的值。
由于您的JSON有效负载有些复杂,因此我们需要使用Logic App表达式从其中提取值。
我使用的有效载荷:
{
"status": "success",
"pages": [{
"number": 1,
"keyValuePairs": [{
"key": [{
"text": "Page No:",
"boundingBox": [
507.4
]
}],
"value": [{
"text": "1",
"boundingBox": [
586.8
],
"confidence": 1.0
}]
},
"null"
]
}]
}
一旦从上游操作获得JSON输出,就必须对其进行解析。
现在,初始化几个变量以从解析的JSON中获取所需的值。
要在第一个变量中使用的表达式:concat(body('Parse_JSON')?['pages'] [0]?['keyValuePairs'] [0]?['key' ] [0]?['text'],body('Parse_JSON')?['pages'] [0]?['keyValuePairs'] [0]?['value'] [0]?['text'] )
要在第二个变量中使用的表达式:body('Parse_JSON')?['pages'] [0]?['keyValuePairs'] [0]?['value'] [ 0]?['信心']
最后,您的表单识别器之后的逻辑应用程序将如下所示:
希望对您有帮助!