Liquid模板映射问题:无法解析Product下二级重复Order记录(Logic Apps)
修复Logic Apps中Liquid模板解析二级重复Order记录的问题
常见场景示例
输入XML
<Root> <Product> <Name>Sample Product</Name> <Order> <OrderId>ORD001</OrderId> <Amount>100</Amount> </Order> <Order> <OrderId>ORD002</OrderId> <Amount>200</Amount> </Order> </Product> </Root>
期望JSON输出
{ "productName": "Sample Product", "orders": [ { "orderId": "ORD001", "amount": 100 }, { "orderId": "ORD002", "amount": 200 } ] }
存在问题的Liquid模板
{ "productName": "{{content.Root.Product.Name}}", "orders": { "orderId": "{{content.Root.Product.Order.OrderId}}", "amount": {{content.Root.Product.Order.Amount}} } }
问题说明:直接访问Order节点时,Liquid默认仅读取第一个节点,且生成的是单个JSON对象而非数组,无法匹配期望输出。
修复后的Liquid模板
{ "productName": "{{content.Root.Product.Name}}", "orders": [ {% for order in content.Root.Product.Order %} { "orderId": "{{order.OrderId}}", "amount": {{order.Amount}} }{% unless forloop.last %},{% endunless %} {% endfor %} ] }
二级重复记录处理建议
- 强制使用循环遍历:当XML存在多个同级重复节点时,Liquid会将其解析为数组,必须通过
{% for %}循环遍历每个元素,不能直接单个节点访问。 - 处理JSON格式细节:用
{% unless forloop.last %},{% endunless %}规避循环末尾的多余逗号,避免JSON格式校验失败。 - 确认XML结构一致性:确保重复节点直接嵌套在父节点下,若XML含命名空间,需在Logic Apps的XML转JSON步骤中指定命名空间,或在Liquid模板中通过命名空间前缀访问节点(如
{{content:Root.Product:Order}},前缀需匹配实际命名空间)。 - 调试辅助:在Logic Apps中添加
Compose操作,输出解析后的XML对象结构,直观确认重复节点是否被识别为数组,再调整模板的遍历路径。
内容的提问来源于stack exchange,提问作者SMSVikasK
相关产品推荐
相关产品推荐

