JSON Arrays

,When working with JSON arrrays in Power Automate, the Apply to Each loop is often assumed to be necessary when transforming or manipulating arrays.

However, loops are not always the best option and they can lead to performance bottlenecks and overly complex flows.

Instead, you can use the Select action which provides a streamlined way to transform arrays without iterative loops.

When working with JSON, the Select action is your friend! This article explains why, how, and when you can use the Select action to optimise your flows, saving time and improving efficiency.

json arrays

Option 1: Apply To Each

Loop actions iterate over each item in an array, enabling you to perform an operation on each element. If you need iterative processing, such as sending an email for each item in the array, or to add each array to Dataverse, then loops are the way to go.

However, if you are using loops to manipulate, transform or filter arrays, they have downsides.

Large datasets can significantly increase the runtime of your flow, as each iteration requires individual processing. Unless concurrency is enabled (and used carefully), loops execute sequentially, which can slow down the flow.

Flows can become cluttered with nested actions, making them harder to understand, maintain and debug.

Option 2: Select

The Select action offers an elegant way to manipulate arrays in a single step by mapping or transforming elements. Instead of iterating over items one by one, as is the case with a loop, the Select action processes the entire array at once and outputs a transformed array. The result is a cleaner, faster flow.

The Select action is ideal when you need to flatten, simplify or filter JSON arrays or rename keys and iterative processing is not required, such as sending an email for each item in the array.

 

Example: Transforming JSON Data

Here is the scenario. You have the following JSON array:

[
{ "id": 1, "firstname": "Alice", "lastname": "Turner", "role": "Manager" },
{ "id": 2, "firstname": "Bob", "lastname": "Smith", "role": "Developer" },
{ "id": 3, "firstname": "Charlie", "lastname": "Brown", "role": "Analyst" }
]

You want to extract only the name and role fields, producing a new array like this:

[
{ "name": "Alice Turner", "role": "Manager" },
{ "name": "Bob Smith", "role": "Developer" },
{ "name": "Charlie Brown", "role": "Analyst" }
]

Using Apply to Each

With an Apply to Each, you would:

1. Loop through the array.

2. Use Compose or Append to array to build the new array.

While functional, this approach is verbose and inefficient for large datasets.

Using Select

With the Select action:

1. Provide the array as input.

2. Map the fields:

name: concat(item()?['firstname'], ' ', item()?['lastname'])
role: item()?['role']

The result is the desired transformed array in a single step. No loops required!

Summary

The Select action is a game-changer for handling JSON arrays in Power Automate. It avoids the performance and complexity pitfalls of the Apply to Each loop.

Whenever you find yourself reaching for a loop to manipulate JSON, pause and consider if the Select action can achieve the same result more efficiently. Your flows – and your future debugging self – will thank you!

Feel free to share your experiences or questions about using JSON in Power Automate in the comments below. And if you’re new to JSON in Power Automate, check out my previous articles:

Leave a Comment

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

Scroll to Top