How to Convert an Object to Array in Power Automate

Do you need to convert a JSON object to an array in your flow?

Most of the data you deal with in your flows will be a complex JSON object.

For example, when you get items from a SharePoint list you might only be interested in the names or emails within a multiple choice person field, but these will be nested in an object of records and arrays.

How can you get these names or emails into an array to use in other parts of your flow?

This post will show you how to convert your complex objects to a simple array in Power Automate.

JSON Object to Convert to Array

{
  "value": [
    {
      "department": "Accounting",
      "employees": [
        {
          "name": "David Waterson",
          "jobTitle": "Senior Accountant"
        }
      ]
    },
    {
      "department": "IT",
      "employees": [
        {
          "name": "Cory Harding",
          "jobTitle": "DevOps Engineer"
        },
        {
          "name": "Debby Fawn",
          "jobTitle": "Director"
        }
      ]
    }
  ]
}

Above is the sample JSON object that the example in the post will use and its been placed inside a Compose action renamed to Sample Object.

["David Waterson","Cory Harding","Debby Fawn"]

This post is going to show you how to convert this object to an array that contains only the names like the above array.

The sample object is made up of an array named value. Each record in the value array has a field named Employees. The Employees field is itself an array that contains a Name field.

The Name field of interest in this object is nested inside two arrays. These are defined by the square brackets []. This means to create an array with these names you will need to use two nested Apply to each step to loop through each of the items in these arrays.

You will need to loop through the value array and then for each item in the value array you need to loop through the Employee array to get all the names.

Parse JSON with the Parse JSON Action

This is an optional step, but it will make it possible to reference the parts of your object with dynamic content.

Without this, you will still be able to reference the records and arrays in your object, but they just won’t show up in the dynamic content selection.

  1. Add the Parse JSON action to your flow.
  2. Add the dynamic content reference for the JSON data into the Content input.
  3. Click the Generate from sample button.
  1. Paste in a sample of the JSON to parse. In this case, it’s the example JSON in the compose action.
  2. Press the Done button.

This will populate the Schema in the Parse JSON action. The Schema is just an outline of the JSON structure that the flow will expect and includes things like the field names and data types for these fields.

Now you will be able to reference the parts or the object such as the value and employees arrays from the dynamic content menus in the flow builder.

Initialize Array Variable

Next, you will need to create an array variable to hold the names in your object.

  1. Add the Initialize variable action to the flow.
  2. Add a name such as arrNames to the Name input.
  3. Select the Array option in the Type dropdown.
  4. Leave the Value input blank.

Add the Apply to Each Steps

Now you can loop through the different arrays in your object and append the names to your arrNames array variable.

  1. Add an Apply to each step to the flow.
@{outputs('Sample_Object')?['value']}
  1. Add the above reference to your object in the Select an output from previous steps field. Here outputs('Sample_Object') is the JSON object in the compose action named Sample Object and value is the name of the first outer array in the object.
  2. Add a second Apply to each step inside the first Apply to each step.
@{items('Apply_to_each')?['employees']}
  1. Add the above reference to your object in the Select an output from previous steps field of the second Apply to each step. This gets the employees array from the current item of the outer Apply to each step.

📝 Note: Steps 2 and 4 can be replaced with the dynamic content from the Parse JSON action for the value and employees if you used this optional action in the flow.

Append to Array Variable

Now you will be able to add the Append to array variable action to collect all the names during the Apply to each loops.

  1. Add the Append to array variable action inside the second Apply to each step.
  2. Select the arrNames variable in the Name dropdown.
@{items('Apply_to_each_2')?['name']}
  1. Add the above expression to the Value field of the Append to array variable action.

This references the current item in the second Apply to each step and gets only the name from it.

If using the Parse JSON step, this will also be available as dynamic content.

[
  "David Waterson",
  "Cory Harding",
  "Debby Fawn"
]

When the flow runs the Append to array variable action will add each name from the object to the arrNames array variable and result in the above array.

Conclusions

Working with objects in your flows will be a common situation and you might need to extract parts of the object data into an array.

Your object structure probably won’t be the same, but you can apply the same principles to convert the object data to an array.

Have you come across the need to use an array instead of an object in your flow logic?

About the Author

John MacDougall

John MacDougall

John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

Related Articles

Comments

1 Comment

  1. YES

    After Parsing the JSON it will only output “Body” though, which means this is either outdated or there is an error. So I dont understand how I am supposed to take one of the values to be outputted in apply for each. Anyone know how to fix that?

Get the Latest Tech Tips

Write For Us

Are you a tech enthusiast with a talent for writing great content? Come write for us!

Follow Us

Follow us on social media to stay up to date with the latest in tech!