Azure Logic Apps拼接结果异常,请求修正以获取目标输出
Azure Logic Apps 输出格式修正方案
输入JSON
[{ "ACTIVITYID": "0002868073", "STATUS": "Released", "COMMENTS": [{ "COMMENT": "Comment_1" }, { "COMMENT": "Comment_2" }] }, { "ACTIVITYID": "0002868071", "STATUS": "Approved", "COMMENTS": [{ "COMMENT": "Comment_3" }, { "COMMENT": "Comment_4" }] }]
期望输出
0002868073 Released Comment_1 Comment_2 0002868071 Approved Comment_3 Comment_4
现有问题分析
当前逻辑存在两个核心问题:
- 变量未重置:所有字符串变量全局初始化,每次循环不会清空,导致评论内容跨循环累积,出现重复拼接。
- 缺少分隔符:拼接ACTIVITYID、STATUS和评论时无空格分隔,不同活动条目之间也没有换行,格式混乱。
修正步骤
- 精简变量:仅保留
FinalOutput(存储最终结果)和CurrentComments(存储当前活动的评论拼接内容),移除冗余变量。 - 循环内重置变量:每个Activity循环开始时,将
CurrentComments重置为空字符串,避免内容残留。 - 带空格拼接评论:评论循环中直接为每个评论添加空格,最后用
trim()去除末尾多余空格。 - 添加分隔符与换行:拼接最终条目时在各字段间加空格,每个活动条目结束后添加换行符。
- 调整初始化逻辑:将
CurrentComments的初始化放在Activity循环内部,确保每次循环从头开始。
修正后的代码视图
{ "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "For_each_-_ActivityLoop": { "actions": { "Initialize_CurrentComments": { "inputs": { "variables": [{ "name": "CurrentComments", "type": "string", "value": "" }] }, "runAfter": {}, "type": "InitializeVariable" }, "For_each_-_CommentsLoop": { "actions": { "Append_to_CurrentComments": { "inputs": { "name": "CurrentComments", "value": "@concat(items('For_each_-_CommentsLoop')['COMMENT'], ' ')" }, "runAfter": {}, "type": "AppendToStringVariable" } }, "foreach": "@items('For_each_-_ActivityLoop')['COMMENTS']", "runAfter": { "Initialize_CurrentComments": ["Succeeded"] }, "type": "Foreach" }, "Append_to_FinalOutput": { "inputs": { "name": "FinalOutput", "value": "@concat(items('For_each_-_ActivityLoop')['ACTIVITYID'], ' ', items('For_each_-_ActivityLoop')['STATUS'], ' ', trim(variables('CurrentComments')), '\n\n')" }, "runAfter": { "For_each_-_CommentsLoop": ["Succeeded"] }, "type": "AppendToStringVariable" } }, "foreach": "@body('Parse_JSON')", "runAfter": { "Parse_JSON": ["Succeeded"], "Initialize_FinalOutput": ["Succeeded"] }, "type": "Foreach" }, "Initialize_FinalOutput": { "inputs": { "variables": [{ "name": "FinalOutput", "type": "string", "value": "" }] }, "runAfter": {}, "type": "InitializeVariable" }, "Parse_JSON": { "inputs": { "content": "@triggerBody()", "schema": { "items": { "properties": { "ACTIVITYID": { "type": "string" }, "COMMENTS": { "items": { "properties": { "COMMENT": { "type": "string" } }, "required": ["COMMENT"], "type": "object" }, "type": "array" }, "SAPUSERID": { "type": "string" }, "STATUS": { "type": "string" } }, "required": ["ACTIVITYID", "STATUS", "SAPUSERID", "COMMENTS"], "type": "object" }, "type": "array" } }, "runAfter": { "Initialize_FinalOutput": ["Succeeded"] }, "type": "ParseJson" } }, "contentVersion": "1.0.0.0", "outputs": { "FinalResult": { "type": "string", "value": "@trim(variables('FinalOutput'))" } }, "parameters": {}, "triggers": { "manual": { "inputs": { "method": "POST", "schema": { "items": { "properties": { "ACTIVITYID": { "type": "string" }, "COMMENTS": { "items": { "properties": { "COMMENT": { "type": "string" } }, "required": ["COMMENT"], "type": "object" }, "type": "array" }, "SAPUSERID": { "type": "string" }, "STATUS": { "type": "string" } }, "required": ["ACTIVITYID", "STATUS", "SAPUSERID", "COMMENTS"], "type": "object" }, "type": "array" } }, "kind": "Http", "type": "Request" } } }, "parameters": {} }
修正说明
- 每个Activity循环初始化
CurrentComments,确保评论内容每次从头拼接。 - 评论拼接时自动添加空格,最后用
trim()清理多余空格,保证格式整齐。 - 用
AppendToStringVariable累积每个活动条目,通过\n\n实现条目间换行。 - 输出时用
trim()去除末尾多余换行,最终格式完全匹配预期。
内容的提问来源于stack exchange,提问作者Satish Jadhav
相关产品推荐
相关产品推荐

