How can I merge the outputs from a For_Each loop in an Azure Logic App to a single flat array?

Jude Fisher picture Jude Fisher · May 5, 2017 · Viewed 7.4k times · Source

I have a For_Each loop in an Azure Logic App that calls another, nested, Logic App. The result from each iteration of the nested Logic Apps is a JSON object that contains an array of strings, like this:

{
 "Results": ["string a", "string b"]
}

So the output from my For_Each loop in the parent Logic App looks like this:

[
 {"Results": ["string a", "string b"]},
 {"Results": ["string c", "string d"]}
]

I want to put all these strings into a single flat list that I can pass to another action.

How can I do this? Is it possible using the workflow definition language and built-in functions, or do I need to use an external function (in a service, or an Azure Function)?

Answer

Grazi Bonizi picture Grazi Bonizi · Dec 12, 2017

There's a simpler solution, working with Array Variables. At the top level, outside the For Each loop, declare a variable with an InitializeVariable action:

"Initialize_Items_variable": {
    "inputs": {
        "variables": [
            {
                "name": "Items",
                "type": "Array",
                "value": []
            }
        ]
    },
    "runAfter": {},
    "type": "InitializeVariable"
}

Inside the For Each, use a AppendToArrayVariable action. You can append the Response object of the Nested Logic App you just called.

"Append_to_Items_variable": {
    "inputs": {
        "name": "Items",
        "value": "@body('Nested_Logic_App_Response')"
    },
    "runAfter": {
    },
    "type": "AppendToArrayVariable"
}

Hope it helps.