Unnest nested arrays in Power Automate

We all know this problem, you have a nested array in Power Automate but how do we unnest these arrays? It is not easy!

Nested Arrays

Nested arrays can appear in many places. You can also call them arrays of arrays. Quite often you might be processing multiple batches of data.

In the past I have looked at avoiding single item nested arrays. Today I’m looking at the more complicated merging of nested arrays.

In my example I’m going to use the following array:

[
  [
    {
      "title": "rec1"
    },
    {
      "title": "rec2"
    },
    {
      "title": "rec3"
    }
  ],
  [
    {
      "title": "rec4"
    },
    {
      "title": "rec5"
    },
    {
      "title": "rec6"
    }
  ],
  [
    {
      "title": "rec7"
    },
    {
      "title": "rec8"
    },
    {
      "title": "rec9"
    }
  ]
]

In my example flow I’m going to set a compose action to the above array.

Each of the {“title”: “rec1”} sections I will refer to as a record and there are 3 batches of 3 records in my example.

Pieter’s method

Remember Pieter’s method? Well I’m going to make use of this.

Inside an Apply to each I’m creating a Compose. This compose uses the following expression

replace(string(items('Apply_to_each')),'"','''')

This is the expression that will replace every double quote with a single quote. This quote replacement is very important as you will find escaped double quotes that will appear as \” in the output in the flows. To avoid this I’m replacing the double quotes to single quotes.

Then the second Compose references the compose inside the apply to each as Described in my Pieter’s method post. But this time it does a little bit more.

json(replace(replace(string(outputs('Compose_3')),'[{','{'),'}]','}'))

The [ and the ] around the batches will now be removed and we end up with just the records of the nested array that we started off with.

We should probably have a look at the flow runs at the moment. The apply to each generates texts that look like a json array ( but they are not!). They are just text.

The compose after the apply to each will show you that we now have an array with 3 items. And each item is a text matching the texts that we saw inside the apply to each.

De-nest the nested array

So far so good! Picking up the process from the Compose 2 action …

We now have a text that represents an array with all 9 items. But the opening and closing brackets of the array are missing still

The above Join picks up each set of 3 texts and adds a comma in between.

In front of that all we need to add a [ and after the joint texts we add a ]. We can do this in the final compose. This is also where we turn the whole construction back into a json array.

json(concat('[',body('Join'),']'))

Remember that we replaced the double quotes with single quotes. The json function is actually happy to accept either format. Therefore there is no need to convert back to double quotes.

Looking at the output we now have a single array instead of a nested array.

Share
Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

Recent Posts

8 top reasons to use Dataverse or SharePoint lists in the Power Platform

Why would you use Dataverse, if SharePoint lists gives you the option to avoid premium…

6 days ago

Filter by calculated fields in SharePoint using Power Automate

When you read items from a SharePoint list and you want to filter by calculated…

1 week ago

Expected String but got Null, when calling a flow from a Model Driven app

A few weeks ago I looked at calling flows from model driven apps. And for…

1 week ago

Get all your broken connections using Power Automate

To get all your broken connections in the Power Platform can be a challenge. In…

2 weeks ago

Customer Voice a.k.a. Forms Pro the database behind the magic

If you call Customer Voice still Forms Pro or not, have you ever looked into…

2 weeks ago

Many running flows in Power Automate

Recently I noticed that some of my flows end up with many running flows. Types…

3 weeks ago
%%footer%%