Azure Logic Apps中dotLiquid处理SQL Execute Query嵌套数组的语法
正确dotLiquid模板写法
你可以直接用以下两种模板实现需求,按需选择即可:
适配常规场景(SQL返回结果仅含1组内层数组)
绝大多数情况下SQL Execute Query返回的第一层数组只有1个元素,直接取索引为0的内层数组遍历即可,写法最简洁:
{ "products": [ {% for product in content[0] %} { "productId": {{ product.ProductID }}, "productName": "{{ product.ProductName | escape }}" }{% unless forloop.last %},{% endunless %} {% endfor %} ] }
适配兼容场景(第一层数组可能存在多组内层数组)
如果要兼容第一层数组包含多个内层数组的情况,把所有产品数据合并输出,可以用嵌套遍历的写法:
{ "products": [ {% assign first_item = true %} {% for inner_arr in content %} {% for product in inner_arr %} {% unless first_item %},{% endunless %} { "productId": {{ product.ProductID }}, "productName": "{{ product.ProductName | escape }}" } {% assign first_item = false %} {% endfor %} {% endfor %} ] }
关键语法说明
content是Logic Apps的Liquid转换动作默认的输入根对象,对应你传入的SQL查询返回的JSON数据- 用
forloop.last或者自定义first_item标记来控制逗号输出,避免生成的JSON出现语法错误 - 字符串类型字段需要用双引号包裹,搭配
escape过滤器可以处理产品名里的引号、特殊字符,避免JSON格式异常 - 字段映射时直接修改键名即可实现从大驼峰
ProductID到小驼峰productId的转换
内容的提问来源于stack exchange,提问作者SvenAelterman
相关产品推荐
相关产品推荐

