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.
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 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 Parameter | Value |
propertyParentPath | @body() |
propertySubPath | peopleObject |
newPropertyPath | @body().peopleArray |
keyName | Name |
valueName | Details |
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 Parameter | Value |
propertyParentPath | @body().sets |
propertySubPath | people |
newPropertyPath | @item().people |
keyName | Name |
valueName | Details |
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"
}
},
]
}
]
}
Please note that since the parent sets
is a collection, the tranformation 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.
Connector | Scenario |
Planner | Transform an object of references into an array of references in the reponse for 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 | List of actions and triggers to which the policy will apply to. If no operation is selected, this policy will apply to all operations. |