How to merge arrays in Power Automate?

In this Power Automate tutorial, we will see how to merge two arrays. Also, we will see in two different ways to merge arrays in Power Automate, i.e., with apply to each action and without application to each action.

Here we will take the two arrays below (screenshot) and merge them into one. We will do this by two methods:

  • With apply to each action
  • Without apply to each action
Power automate merge arrays into one array

Power automate merge arrays (with apply to each action)

Here we will see how to merge arrays into one using Power Automate.

We will loop through multiple arrays using Apply to each action and then merge the multiple arrays into one with the help of Append to array variable action.

Step 1:  Log in to Power Automate and click +Create in left navigation -> select Instant Cloud Flow.

merge arrays into one in Microsoft Power automate

Then provide the flow name, and select the Manually trigger a flow. Then click on Create.

How to merge arrays into one in Microsoft Power automate

Now, you can see Manually trigger a flow action is added to the flow page.

Power automate merge two arrays

Step 2: Now we will initialize 2 array variables for the person name (Array 1) and Person age (Array 2).

So, click on the +New step -> select Initialize variable action, and provide the below information:

NameTypeValue
Person nameArray[
{
“id”: 1,
“name”: “John”
},
{
“id”: 2,
“name”: “Jane”
},
{
“id”: 3,
“name”: “Michael”
}
]
Person ageArray[
{
“id”: 1,
“age”: 30
},
{
“id”: 2,
“age”: 25
},
{
“id”: 3,
“age”: 40
}
]
Person detailsArray[Result]
Microsoft Power automate merge arrays

Step 3: Now, we will merge the two arrays. For this, we will loop through every entry in the first array and a ‘Filter array’ action to locate the corresponding entry in the second array.

So, click on the +New step -> select Apply to each action. Then provide the below information:

  • Select an output from previous steps: Provide person’s name from dynamic content.

Next, click on Add an action -> select Filter array action. Then provide the below information:

  • From: Select Person age from the dynamic content.
  • Choose a value: Provide the below expression:
item()?['id'] 
  • Operator: Select the operator as is equal to
  • Choose a value: Provide the below expression:
items('Apply_to_each')?['id']

After that, click on Add an action and select Append to array variable action. Then provide the below information:

  • Name: Select the Product details from the drop-down.
  • Value Provide the below value:
{
  "id": "@{item()?['id']}",
  "name": "@{item()?['name']}",
  "age": "@{first(body('Filter_array'))?['age']}"
}
Microsoft Power automate merge arrays into one

Step 4: Next, we will create an Html table from the final array, so click on the +New step ->select Create HTML table action. Then provide the below information:

  • From: Select the Person details variable from dynamic content.
Power automate merge arrays into one

Step 5: Next, we will send the above Html table in an email, so, click on the +New step -> select Send an email (V2) action. Then provide the below information:

  • To: Provide the recipient email
  • Subject: Provide the subject of an email
  • Body: Select the output of the Html table action from the dynamic content.
How to merge arrays in Power Automate

Step 6: Now run the flow manually, for this, save the flow, click on the Test icon ->select the Manually option -> click on the Test button. You can see your flow run successfully.

merge arrays in Power Automate

Power automate merge arrays (without apply to each action)

Here we will see how to merge arrays without Apply to each action in Power Automate.

We will merge two arrays without using “Apply to each” in Microsoft Power Automate by using the “xml()” method in along with the Select action to efficiently combine the arrays without the requirement for looping, reducing performance impact, particularly with large datasets.

Step 1:  Log in to Power Automate and click +Create in left navigation -> select Instant Cloud Flow.

merge arrays into one in Microsoft Power automate

Then provide the flow name, and select the Manually trigger a flow. Then click on Create.

How to merge arrays into one in Microsoft Power automate

Now, you can see Manually trigger a flow action is added to the flow page.

Power automate merge two arrays

Step 2: Now we will initialize 2 array variables, for person name (Array 1) and Person age (Array 2) last one for Person details (final array).

NameTypeValue
Person nameArray[
{
“id”: 1,
“name”: “John”
},
{
“id”: 2,
“name”: “Jane”
},
{
“id”: 3,
“name”: “Michael”
}
]
Person ageArray[
{
“id”: 1,
“age”: 30
},
{
“id”: 2,
“age”: 25
},
{
“id”: 3,
“age”: 40
}
]

So, click on the +New step -> select Initialize variable action, provide the below information:

Power automate merge arrays

Step 3: Now we will convert Array 2 to XML array with root element, so click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: Provide the below code as input.
{
"root": {
"Array": @{variables('Person Age')}
}
}
MS Power automate merge arrays

Step 4: Now we will use XML() to convert the above array output into XML format, so click on the +New step -> select Compose action. Then provide the below information:

  • Inputs: Then provide the below expression:
xml(outputs('Compose'))
 merge arrays in Power Automate

Step 5: Now we will merge both the array, that means we will add second array items to the first array.

So, click on the +New step and select ‘Select’ action. Then provide the below information.

  • From: Provide the Person name from dynamic content.
  • Map: Then in this field click on the ‘T’ icon, to switch to the text mode. Then provide the below code:
{
  "Id": @{item()?['id']},
  "Name": @{item()?['name']},
  "Age": @{xpath
(
  outputs('Compose-XML'), 
  concat('//Array[id/text()="', item()['id'], '"]/age/text()')
)?[0]}
}
 merge arrays in Microsoft Power Automate

Step 6: Next, we will create an Html table from the final array, so, click on the +New step ->select Create HTML table action. Then provide the below information:

  • From: Select the output of the selected action from dynamic content.
How to merge arrays in Microsoft Power Automate

Step 7: Next, we will send the above Html table in an email, so, click on the +New step -> select Send an email (V2) action. Then provide the below information:

  • To: Provide the recipient email
  • Subject: Provide the subject of an email
  • Body: Select the output of the Html table action from the dynamic content.
How to merge arrays in Power Automate

Step 8: Now run the flow manually; for this, save the flow, click on the Test icon ->select the Manually option -> click on the Test button. You can see your flow run successfully.

merge arrays into one in Power automate

Conclusion

In this Power Automate tutorial, we will see how to merge arrays into one using Power Automate.

You may also like:

>