如何在Azure Logic Apps中移除数组空对象及优化JSON构造?
移除JSON数组中的空对象/空数组方案
问题背景
需要将包含空数组的tags数组处理后发送至下一个HTTP请求,示例数据如下:
{ "documentId": "87f1bf4b-190a-40dd-b155-89b7530160ad", "tags": [ { "Title": "Expiration Date", "AKA": "", "Value": "September 25, 1980", "Content": "Expired September 25, 1980", "path": "//Document/Sect/P", "page": 0 }, [], { "Title": "Expiration Date", "AKA": "", "Value": "September 25, 1980", "Content": "Expired September 25, 1980", "path": "//Document/Sect/P", "page": 0 } // 其余元素省略 ] }
当前通过手动拼接JSON的方式构造数据,但会引入null或空数组,需要更可靠的方式避免这类空对象。
当前构造方式
{ "documentId": @{triggerBody()?['Name']}, "tags": [ @{if(equals(length(json(body('HTTP').results)),0), null,json(body('HTTP').results)[0])}, @{if(equals(length(json(body('HTTP_2').results)),0), json(body('HTTP_2').results),json(body('HTTP_2').results)[0])}, @{json(body('HTTP_3').results)[0]}, @{json(body('HTTP_4').results)[0]}, @{json(body('HTTP_5').results)[0]}, @{json(body('HTTP_6').results)[0]}, @{json(body('HTTP_7').results)[0]}, @{json(body('Method_2').results)[0]}, @{json(body('Method_3').results)[0]}, @{json(body('Method_4').results)[0]} ] }
解决方案
方案1:构造数组时仅添加有效元素
使用createArray结合条件判断,只将非空的元素加入数组,从根源避免空值/空数组:
{ "documentId": @{triggerBody()?['Name']}, "tags": @{createArray( if(greater(length(json(body('HTTP').results)),0), json(body('HTTP').results)[0], null), if(greater(length(json(body('HTTP_2').results)),0), json(body('HTTP_2').results)[0], null), if(greater(length(json(body('HTTP_3').results)),0), json(body('HTTP_3').results)[0], null), if(greater(length(json(body('HTTP_4').results)),0), json(body('HTTP_4').results)[0], null), if(greater(length(json(body('HTTP_5').results)),0), json(body('HTTP_5').results)[0], null), if(greater(length(json(body('HTTP_6').results)),0), json(body('HTTP_6').results)[0], null), if(greater(length(json(body('HTTP_7').results)),0), json(body('HTTP_7').results)[0], null), if(greater(length(json(body('Method_2').results)),0), json(body('Method_2').results)[0], null), if(greater(length(json(body('Method_3').results)),0), json(body('Method_3').results)[0], null), if(greater(length(json(body('Method_4').results)),0), json(body('Method_4').results)[0], null) )} }
随后添加Filter数组动作,过滤掉数组中的null值:
- 输入数组:
body('Create_JSON')?['tags'] - 过滤条件:
item() is not equal to null
方案2:用表达式直接过滤已构造的数组
如果已经有包含空值的数组,可以用filter表达式一次性移除空对象/空数组:
{ "documentId": @{triggerBody()?['Name']}, "tags": @{filter( createArray( json(body('HTTP').results)[0], json(body('HTTP_2').results)[0], json(body('HTTP_3').results)[0], json(body('HTTP_4').results)[0], json(body('HTTP_5').results)[0], json(body('HTTP_6').results)[0], json(body('HTTP_7').results)[0], json(body('Method_2').results)[0], json(body('Method_3').results)[0], json(body('Method_4').results)[0] ), not(or(equals(item(), null), equals(length(item()), 0))) )} }
该表达式会自动过滤掉null值和长度为0的空数组。
方案3:分步收集有效元素
- 添加Initialize variable动作,创建类型为
Array的变量validTags - 对每个HTTP请求结果添加Condition动作:
- 条件:
length(json(body('HTTP').results)) > 0 - 如果为真,执行Append to array variable动作,将
json(body('HTTP').results)[0]加入validTags
- 条件:
- 最后构造JSON时直接使用该变量:
{ "documentId": @{triggerBody()?['Name']}, "tags": @{variables('validTags')} }
这种方式逻辑清晰,适合复杂判断场景。
内容的提问来源于stack exchange,提问作者edoms06
相关产品推荐
相关产品推荐

