Search code examples
azureazure-logic-apps

Append result in Azure Logic Apps


I have below json input to logic apps

[
  {
    "ACTIVITYID": "0002868073",
    "STATUS": "Released",
    "COMMENTS": [
      {
        "COMMENT": "Comment_1"
      },
      {
        "COMMENT": "Comment_2"
      }
    ]
  },
  {
    "ACTIVITYID": "0002868071",
    "STATUS": "Approved",
    "COMMENTS": [
      {
        "COMMENT": "Comment_3"
      },
      {
        "COMMENT": "Comment_4"
      }
    ]
  }
]

I want output like

0002868073 Released Comment_1 Comment_2

0002868071 Approved Comment_3 Comment_4

Here is my logic apps code view

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each_-_ActivityLoop": {
                "actions": {
                    "Append_to_string_variable_2": {
                        "inputs": {
                            "name": "FinalComment",
                            "value": "@variables('AppendInnerComment')"
                        },
                        "runAfter": {
                            "For_each_-_CommentsLoop": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToStringVariable"
                    },
                    "For_each_-_CommentsLoop": {
                        "actions": {
                            "Append_to_string_variable": {
                                "inputs": {
                                    "name": "InnerComment",
                                    "value": "@items('For_each_-_CommentsLoop')['COMMENT']"
                                },
                                "runAfter": {},
                                "type": "AppendToStringVariable"
                            },
                            "Append_to_string_variable_3": {
                                "inputs": {
                                    "name": "AppendInnerComment",
                                    "value": "@variables('InnerComment')"
                                },
                                "runAfter": {
                                    "Append_to_string_variable": [
                                        "Succeeded"
                                    ]
                                },
                                "type": "AppendToStringVariable"
                            }
                        },
                        "foreach": "@items('For_each_-_ActivityLoop')['COMMENTS']",
                        "runAfter": {},
                        "type": "Foreach"
                    },
                    "Set_variable_2": {
                        "inputs": {
                            "name": "FinalOutput",
                            "value": "@{items('For_each_-_ActivityLoop')['ACTIVITYID']}@{items('For_each_-_ActivityLoop')['STATUS']}@{variables('FinalComment')}"
                        },
                        "runAfter": {
                            "Append_to_string_variable_2": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    }
                },
                "foreach": "@body('Parse_JSON')",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_FinalComment": {
                "inputs": {
                    "variables": [
                        {
                            "name": "FinalComment",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_variable_-_AppendInnerComment": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_FinalOutput": {
                "inputs": {
                    "variables": [
                        {
                            "name": "FinalOutput",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_FinalComment": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_InnerComment": {
                "inputs": {
                    "variables": [
                        {
                            "name": "InnerComment",
                            "type": "string"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_FinalOutput": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_variable_-_AppendInnerComment": {
                "inputs": {
                    "variables": [
                        {
                            "name": "AppendInnerComment",
                            "type": "string"
                        }
                    ]
                },
                "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_InnerComment": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "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": {}
}

However, I get the output:

0002868073 Released Comment_1 Comment_1 Comment_2

0002868071 Approved Comment_1Comment_1Comment_2Comment_1Comment_1Comment_2Comment_1Comment_2Comment_3Comment_1Comment_2Comment_3Comment_4

Please advice to get the desired result.


Solution

  • Is this what you want?

    Result

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "For_Each_Activity": {
                    "actions": {
                        "Append_To_Final_Array": {
                            "inputs": {
                                "name": "Final Array",
                                "value": "@{items('For_Each_Activity')['ACTIVITYID']} @{items('For_Each_Activity')['STATUS']} @{trim(variables('Concatenated Comments'))}"
                            },
                            "runAfter": {
                                "For_Each_Comment": [
                                    "Succeeded"
                                ]
                            },
                            "type": "AppendToArrayVariable"
                        },
                        "For_Each_Comment": {
                            "actions": {
                                "Append_To_Concatenated_Comments": {
                                    "inputs": {
                                        "name": "Concatenated Comments",
                                        "value": "@{items('For_Each_Comment')['COMMENT']} "
                                    },
                                    "runAfter": {},
                                    "type": "AppendToStringVariable"
                                }
                            },
                            "foreach": "@items('For_Each_Activity')['COMMENTS']",
                            "runAfter": {
                                "Set_Concatenated_Comments": [
                                    "Succeeded"
                                ]
                            },
                            "type": "Foreach"
                        },
                        "Set_Concatenated_Comments": {
                            "inputs": {
                                "name": "Concatenated Comments",
                                "value": "@{string('')}"
                            },
                            "runAfter": {},
                            "type": "SetVariable"
                        }
                    },
                    "foreach": "@triggerBody()",
                    "runAfter": {
                        "Initialize_Concatenated_Comments": [
                            "Succeeded"
                        ]
                    },
                    "runtimeConfiguration": {
                        "concurrency": {
                            "repetitions": 1
                        }
                    },
                    "type": "Foreach"
                },
                "Initialize_Concatenated_Comments": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "Concatenated Comments",
                                "type": "string"
                            }
                        ]
                    },
                    "runAfter": {
                        "Initialize_Final_Array": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Initialize_Final_Array": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "Final Array",
                                "type": "array"
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "InitializeVariable"
                },
                "Initialize_Result": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "Result",
                                "type": "string",
                                "value": "@{join(variables('Final Array'), '\r\n')}"
                            }
                        ]
                    },
                    "runAfter": {
                        "For_Each_Activity": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "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": {}
    }