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

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


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.


Picking up on @DerekLi's useful comment above, it seems this is not possible at the time of writing with Logic Apps schema version 2016-06-01.

One of the great strengths of Logic Apps is the ability to leverage the power of Azure Functions to solve problems like this that can't (yet) be solved in the schema language.

Re-writing the array is trivial in c# within a function:

using System.Net;public class Result{    public List<string> Results {get; set;}}public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log){    log.Info("C# HTTP trigger function processed a request.");    var inputs = await req.Content.ReadAsAsync<List<Result>>();    var outputs = new List<string>();    foreach(var item in inputs)    {        log.Info(item.Results.ToString());        outputs.AddRange(item.Results.Where(x => !string.IsNullOrEmpty(x)));    }    return req.CreateResponse(HttpStatusCode.OK, outputs);}

And this function can then be passed the result of the For_Each loop:

"MyFunction": {    "inputs": {                "body": "@body('Parse_JSON')",                "function": {                    "id": "/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Web/sites/{function-app-name}/functions/{function-name}"                },                "method": "POST"            },            "runAfter": {                "For_each": [                    "Succeeded"                ]            },            "type": "Function"}


There is also a way to do it using the workflow definition language. (https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language).

Using the fonctions string and replace you can work on your json as a string rather than on objects.

Here is a Flat_List action that follows a Parse_JSON action with your data:

Your data:

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

Flat_List component:

 "Flat_List": {            "inputs": "@replace(replace(replace(string(body('Parse_JSON')),']},{\"Results\":[',','),'}]','}'),'[{','{')",            "runAfter": {                "Parse_JSON": [                    "Succeeded"                ]            },            "type": "Compose"        },

What happens here? First we use string that takes your json data and gives:

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

We replace all the ]},{"Results":[ by ,.

We replace all the }] by }.

We replace all the [{ by {.

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

Then you are free to parse it back to json with:

"Parse_JSON_2": {                "inputs": {                    "content": "@outputs('Flat_List')",                    "schema": {                        "properties": {                            "Results": {                                "items": {                                    "type": "string"                                },                                "type": "array"                            }                        },                        "type": "object"                    }                },                "runAfter": {                    "Flat_List": [                        "Succeeded"                    ]                },                "type": "ParseJson"            }

You can see it as a proof of concept as the Azure Function may be easier to re-read later but there may be many reason not to want to instantiate a new Azure Function while you can do the job in Logic App.

Feel free to ask for more details if needed :)