Azure Logic Apps. Split array into n-equal arrays

1

Description

Today, I would like to show how to split an array into several equal arrays in Azure Logic Apps. The easiest way for this task will be to write in-line JavaScript code or to use Azure Functions. Of course, there will be cost implications. Therefore, in this article, I will show how you can do it with vanilla Logic Apps functionality.

Data

As example let’s create a simple JSON object that contains an array of data.

{
    "data": [
        {
            "title": "Project 1"
        },
        {
            "title": "Project 2"
        },
        {
            "title": "Project 3"
        },
        {
            "title": "Project 4"
        }
    ]
}

Let’s imagine we need to split this array into 2 equal arrays.

Azure Logic Apps

Let’s start with creating an HTTP request trigger. It will listen a POST request on a generated URL. JSON schema for data provided above is the following:

{
    "type": "object",
    "properties": {
        "data": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "title": {
                        "type": "string"
                    }
                },
                "required": [
                    "title"
                ]
            }
        }
    }
}

Now let’s initialize a new variable that will store only array data. We can do it easily with Logic Apps built in Initialize Variable function:

Now we need to create 2 variables with type array, where we will push data from original Incoming Array.

Plus we need to create one additional variable to control data flow. Let’s call it IndexArray. It will be integer with initial value equal to 1.

Loop through Incoming Array

Now we are ready to implement the logic. The whole idea is loop through incoming array, through each iteration the system should check if IndexArray is === 1, than push data to Array1, else push to Array2. On each iteration we will increment IndexArray. If IndexArray will === 3, override count to 1.

The first part should look something like that:

Plus logic to override the IndexArray

Loop concurrency control

The last thing that we will need to do (and this is something important), is to set concurrency mode for foreach loop to 1. So basically instruct Logic Apps to run this loop in sequential order.

Summary

That’s it! You can expand this solution to more arrays if required. Just in case the whole JSON code is below. Happy coding!

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Condition": {
                        "actions": {
                            "Append_to_array_variable": {
                                "inputs": {
                                    "name": "Array1",
                                    "value": "@items('For_each')"
                                },
                                "runAfter": {},
                                "type": "AppendToArrayVariable"
                            }
                        },
                        "else": {
                            "actions": {
                                "Append_to_array_variable_2": {
                                    "inputs": {
                                        "name": "Array2",
                                        "value": "@items('For_each')"
                                    },
                                    "runAfter": {},
                                    "type": "AppendToArrayVariable"
                                }
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@variables('IndexArray')",
                                        1
                                    ]
                                }
                            ]
                        },
                        "runAfter": {},
                        "type": "If"
                    },
                    "Condition_2": {
                        "actions": {
                            "Set_variable": {
                                "inputs": {
                                    "name": "IndexArray",
                                    "value": 1
                                },
                                "runAfter": {},
                                "type": "SetVariable"
                            }
                        },
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@variables('IndexArray')",
                                        3
                                    ]
                                }
                            ]
                        },
                        "runAfter": {
                            "Increment_variable": [
                                "Succeeded"
                            ]
                        },
                        "type": "If"
                    },
                    "Increment_variable": {
                        "inputs": {
                            "name": "IndexArray"
                        },
                        "runAfter": {
                            "Condition": [
                                "Succeeded"
                            ]
                        },
                        "type": "IncrementVariable"
                    }
                },
                "foreach": "@variables('Incoming Array')",
                "runAfter": {
                    "Initialize_IndexArray": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_Array1": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Array1",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Incoming_Array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Array2": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Array2",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Array1": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Incoming_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Incoming Array",
                            "type": "array",
                            "value": "@triggerBody()?['data']"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_IndexArray": {
                "inputs": {
                    "variables": [
                        {
                            "name": "IndexArray",
                            "type": "integer",
                            "value": 1
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Array2": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {
                        "properties": {
                            "data": {
                                "items": {
                                    "properties": {
                                        "title": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "title"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}
Nazar Martincenco

About

Nazar is a development and DevOps lead in Application & Cloud Practice. In this role, he is leading full-stack software development in .Net Core, C#, React and Azure. His main topics of interest are digital transformation, Cloud and Data Science. Nazar likes working with the most technically complicated projects and always delivers them with high standards. He is also a huge fun and advocate of Azure DevOps & DevOps best practices.

More on Nazar Martincenco.

Related Posts

Your email address will not be published. Required fields are marked *