如何在Azure Logic Apps中使用setProperty合并含数组的JSON对象?
在Azure Logic Apps中合并两个对象的Vehicles数组
问题分析
你之前使用的setProperty(json(string(variables('sampleItem1')))['Vehicles'], json(string(variables('sampleItem2')))['Vehicles'])存在两处问题:
- 多余的类型转换:
variables('sampleItem1')本身就是JSON对象,无需通过json(string())二次转换 - 参数逻辑错误:
setProperty的参数顺序不对,且直接赋值会替换原有数组,而非追加元素
正确实现方式
要将Sample Item2的Vehicles数组追加到Sample Item1的Vehicles数组中,同时保留Sample Item1的其他属性,使用以下表达式即可:
setProperty( variables('sampleItem1'), 'Vehicles', concat(variables('sampleItem1')['Vehicles'], variables('sampleItem2')['Vehicles']) )
表达式说明
setProperty(原对象, 要修改的属性名, 新属性值):用于修改原对象的指定属性concat(数组1, 数组2):将两个数组直接拼接,实现元素追加的效果(如果需要去重合并,可替换为union(数组1, 数组2))
验证结果
执行上述表达式后,将得到你期望的合并结果:
{ "Name": "SomeName ", "Vehicles": [ { "Brand": "Volkswagen", "Color": "Blue" }, { "Brand": "BMW", "Color": "Black" }, { "Brand": "Mercedes", "Color": "White" } ] }
内容的提问来源于stack exchange,提问作者Jay
相关产品推荐
相关产品推荐

