In today’s blog post, I’ll dive into working with arrays and the different methods to extract, combine, join and filter values of multiple arrays. In this guide, you will get to learn some of the most common ways (personal experience) to work with arrays in Power Automate. Whether you’re working with small or large arrays, these techniques will help you understand and work with arrays and help you giving your desired outputs. So let us jump into how to make the most out of these tools and handle arrays like a queen/king! 💪
For the demonstration throughout this article, I’m going to make use of two composed array-objects called “Cars1” and “Cars2” where they both contains an array of car-brands, together with a third array “allCars” which will be referred to during the guide.
Joining arrays excluding duplicates
The basic expression to join two or more arrays, is done by the expression union(). This expression joins the array values, except that when you are joining using this method it is not including the duplicated values, and only appends one line if there are multiple duplicates discovered across the multiple arrays.
Method
union(outputs('Compose_-_Cars1'),outputs('Compose_-_Cars2'))
Result
[
"Tesla",
"Volkswagen",
"Audi",
"Dodge",
"Hyundai",
"Volvo",
"BMW",
"Polestar",
"Honda",
"Skoda",
"Mercedes Benz"
]
Joining arrays including duplicates
There is a method to join arrays while keeping the values, the easiest way is to convert the array to string object, and then concatenate them and replace the brackets in between. Then you need to convert it back to an array object afterwards. All of this can be done using a one-line syntax.
Method
json(replace(concat(string(outputs('Compose_-_Cars1')),string(outputs('Compose_-_Cars2'))),'"]["','","'))
Result
The items named “Volvo” are duplicated in the array
[
"Tesla",
"Volkswagen",
"Audi",
"Dodge",
"Hyundai",
"Volvo",
"BMW",
"Volvo",
"Polestar",
"Honda",
"Skoda",
"Mercedes Benz"
]
Filter arrays based on item values (the easy and expensive way)
When you’re dealing with small arrays (if the length is not in thousands). You may consider to just use this method to filter to specific values using a for each loop. But if you’re dealing with arrays in the length of 1000+, then this method will be expensive based on the Power Automate limitations.
Method
You can also add additional OR conditions for your if statement to match multiple item values.
Result
[
"Volvo",
"Volvo"
]
Filter arrays based on item values (The advanced and cheap way)
We can also accomplish the thing by using the “Filter array” action, as a part of the Data Operations connector.
Method (Single value version)
@contains(item(),'Audi')
Result
[
"Audi"
]
Method (Multiple values version)
@or(equals(item(),'Hyundai'),equals(item(),'Polestar'))
Result
[
"Hyundai",
"Polestar"
]
Find the common array values
If I specifically want to create a list of duplicated items across multiple arrays, I would use the intersection method.
Method
intersection(outputs('Compose_-_Cars1'),outputs('Compose_-_Cars2'))
Result
[
"Volvo"
]
Remove duplicated values in an array
If we want to remove duplicates in a single array, we again refer to the union() method. we can then refer it to compare with itself! Rendering it to remove it’s own duplicated values!
Method
union(variables('allCars'),variables('allCars'))
Result
Duplicated “Volvo” values are removed
[
"Tesla",
"Volkswagen",
"Audi",
"Dodge",
"Hyundai",
"Volvo",
"BMW",
"Polestar",
"Honda",
"Skoda",
"Mercedes Benz"
]
Get the first or last array item value
In some cases we only want either the first or last item in the array.
And in the typical cases where I needed the first array item value, it’s when I’m getting a JSON response body. Especially in those case where you’d often come across key-value pairs nested in an array object. Typically you’d want to select the values directly, in those cases you’d want to use the first() method.
Method (first array item)
first(variables('allCars'))
Result
Tesla
Method (last array item v1)
Sometimes you want to get the last item value in an array
last(variables('allCars'))
Result
Mercedes Benz
Method (last array item v2)
Here’s a second version. This syntax is just a build up to the next method “Get a random array item value“
variables('allCars')?[sub(length(variables('allCars')),1)]
Result
Mercedes Benz
Get a random array item value
If you’d ever get to a scenario where you would want to mix up random greetings before sending a message, store the greetings in an array collection and invoke a random one using the following method.
Method
variables('allCars')?[rand(0,sub(length(variables('allCars')),1))]
Result
*Random*
Get unique values from an array of tables
In some scenarios, your data might return an array of structured tables with metadata. To filter outputs, you can also use the Filter array action to only return unique values of one or multiple specific key values. Consider you have this array structure available:
In the array of tables above, we have two duplicated values of name: Kim, age: 29, car: Tesla. Now I only want a list of people with the car Tesla, but without any duplicated values. For this I use a “Filter array” action with an unionized array as input to remove all the duplicated values.
Method (Single value condition)
From:
union(outputs('Compose_-_CarsTableData'),outputs('Compose_-_CarsTableData'))
Body expression:
@equals(item()?['car'], 'Tesla')
Result
[
{
"name": "Kim",
"age": 29,
"car": "Tesla"
},
{
"name": "Anna",
"age": 30,
"car": "Tesla"
}
]
Method (Multiple value condition)
If want to be more specific I can add the name as a condition as well
From:
union(outputs('Compose_-_CarsTableData'),outputs('Compose_-_CarsTableData'))
Body expression:
@and(equals(item()?['car'], 'Tesla'),equals(item()?['name'], 'Kim'))
Result
[
{
"name": "Kim",
"age": 29,
"car": "Tesla"
}
]
Summary
We’ve learned how to handle arrays in Power Automate, based on the most common scenarios. We’ve also learned how to use the different methods to extract, combine, join and filter values of multiple arrays, helping us to get our desired outputs while keeping the flow clean. Whether you’re working with small or large arrays, this guide explains and identifies the common array techniques and methods that will help you along the way in building your Power Automate!
Leave a Reply