Convert An Object To An Array(Preview)

Template ID: convertobjecttoarray

Converts a JSON object in the request or response body to an array. This template is available in Power Apps and Power Automate.

To start, specify the path to the parent object or collection. Then, specify the property subpath within the parent where the array is located. Next, specify a property name for each child object's key and a new property name for the properties within each child object. Finally, specify the path where the new object property will be written to.

A few things to keep in mind:

  • You can leave the subPath empty if the parent is itself the array that you want to transform.
  • To overwrite the existing array, the path would be the previously specified parent path and property subpath.
  • To keep the specified object key as a property within the new object, set the "Retain key" option to "true" (which is the default if left empty). Otherwise, set the option to "false".

Examples

Let’s look at some examples that use the following parameters.

Example #1

Input JSON:

    {
        "peopleObject":{
            "XYZ":{
                "Age": "30"
            },
            "ABC":{
                "Age": "23"
            }
        }
    }
Input ParameterValue
propertyParentPath@body()
propertySubPathpeopleObject
newPropertyPath@body().peopleArray
keyNameName
valueNameDetails

Output JSON:

    {
        "peopleObject":{
            "XYZ":{
                "Age": "30"
            },
            "ABC":{
                "Age": "23"
            }
        },
        "peopleArray":[
            {
               "Name": "XYZ",
               "Details":{
                   "Age": "30"
               }
            },
            {
               "Name": "ABC",
               "Details":{
                   "Age": "23"
               }
            },
        ]
    }

Example #2

Input JSON

    {
        "sets":[
            {
                "people":{
                    "XYZ":{
                        "Age": "30"
                    },
                    "ABC":{
                        "Age": "23"
                    }
                }
            },
            {
                "people":{
                    "PQR":{
                        "Age": "32"
                    },
                    "MNO":{
                        "Age": "26"
                    }
                }
            },
        ]
    }
Input ParameterValue
propertyParentPath@body().sets
propertySubPathpeople
newPropertyPath@item().people
keyNameName
valueNameDetails

Output JSON:

    {
        "sets":[
            {
                "people":[
                    {
                        "Name": "XYZ",
                        "Details":{
                            "Age": "30"
                        }
                    },
                    {
                        "Name": "ABC",
                        "Details":{
                            "Age": "23"
                        }
                    },
                ]
            },
            {
                "people":[
                    {
                        "Name": "PQR",
                        "Details":{
                            "Age": "32"
                        }
                    },
                    {
                        "Name": "MNO",
                        "Details":{
                            "Age": "26"
                        }
                    },
                ]
            }
        ]
    }

Note that since the parent sets is a collection, the transformation is applied to each subPath people.

Example from open-sourced connectors

We have one instance of this template being used in our open-sourced connectors repository.

ConnectorScenario
PlannerTransform an object of references into an array of references in the response for the Get Task Details action.

Input Parameters

Name Key Required Type Description
Target object or collection path x-ms-apimTemplateParameter.propertyParentPath True String Path to the object or collection.
Property subpath x-ms-apimTemplateParameter.propertySubPath String Property subpath within the object or collection.
Path of the new property x-ms-apimTemplateParameter.newPropertyPath True String Path of the new property.
Property name for the key x-ms-apimTemplateParameter.keyName String Property name for the key.
Property name for the value x-ms-apimTemplateParameter.valueName String Property name for the value.
Run policy on x-ms-apimTemplate-policySection True Enum Specifies when to run this policy
- Request: Policy template will run before request is sent to the backend API.
- Response: Policy will run after receiving response from the backend API.
Operations x-ms-apimTemplate-operationName Array Policy will run on the selected list of actions and triggers. If no operation is selected, this policy will apply to all operations.