Convert An Array To An Object(Preview)

Template ID: convertarraytoobject

Converts an array in the body to a JSON object with elements identified by a user specified key. This template is available in Power Apps and Power Automate.

In this article

To start, specify the path to the parent object or collection and the property subpath within the parent where the array is located. Next, specify a property within each item of the array as the object key. The existence of such a key is necessary for the transformation to make sense as object keys should be unique and that object key is expected to explain the rest of the properties which will become part of the value. 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 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:

    {
        "peopleArray":[
            {
               "Name": "XYZ",
               "Age": "30"
            },
            {
               "Name": "ABC",
               "Age": "23"
            }
        ]
    }
Input ParameterValue
propertyParentPath@body()
propertySubPathpeopleArray
keyWithinCollectionPathName
newPropertyPathpeopleObject
retainKeyfalse

Output JSON:

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

Example 2

Input JSON

    {
        "sets":[
            {
                "people":[
                    {
                        "Name": "XYZ",
                        "Age": "30"
                    },
                    {
                        "Name": "ABC",
                        "Age": "23"
                    }
                ]
            },
            {
                "people":[
                    {
                        "Name": "PQR",
                        "Age": "32"
                    },
                    {
                        "Name": "MNO",
                        "Age": "26"
                    }
                ]
            }
        ]
    }
Input ParameterValue
propertyParentPath@body().sets
propertySubPathpeople
keyWithinCollectionPathName
newPropertyPathpeople
retainKeytrue

Output JSON:

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

Please note that since the parent sets is a collection, the tranformation is applied to each subPath people and the key Name is retained since retainKey is set to true.

Example from open-sourced connectors

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

ConnectorScenario
PlannerTransform an array of references into an object with the id being the key in the request for Update 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 to object key within the array x-ms-apimTemplateParameter.keyWithinCollectionPath True String Subpath to the key within each item of the array.
Path of the new property x-ms-apimTemplateParameter.newPropertyPath True String Path of the new property.
Retain key x-ms-apimTemplateParameter.retainKey Enum Retain key as property.
- true
- false
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 List of actions and triggers to which the policy will apply to. If no operation is selected, this policy will apply to all operations.