I am back with another blog on Power Automate. There is so much Power Automate can offer that you hardly run out of topics to share it through blogs.
And one such topic is the ability to dynamically add/ remove properties from a JSON object. In this blog I am going to discuss the below topics
- Dynamically add/ remove properties from a JSON object using Power Automate.
- Dynamically set a JSON property in Power Automate
- Create a JSON object dynamically in Power Automate
So let’s get started here. While it’s quite easy, some quirks here and there may throw you off. I will first get started with creating the JSON object.
I created a simple Power Automate flow. In the first step, I initialized the JSON object
A very important note here. Don’t keep the value empty when you initialize the JSON object. Set the value to {}. If not it won’t work.
My final intention is to create a JSON object similar to the below format.
{
"Name": "Adrian",
"Address": {
"City": "Milpitas",
"State": "CA"
}
}
Quite easy. So let’s get started. You may be wondering – ‘Can’t I go ahead and initialize at the beginning itself’. Well the answer is Yes. However what if you get a JSON object as input where you need to add remove properties. The below points should help you to achieve the same.
I use ‘Compose – Data Operation’ to create the “Name” property. I have used the function addProperty to achieve the same. Check for the formula in the comments of the action.
The next step is to add the JSON node to the initial JSON using Set variable action step. I use the output of the Compose operation from previous step.
Quite easy. In the same way you can use to create an integer, decimal, boolean property. But how about adding another JSON object as a node property? We can do that as well using the same addProperty function.
For this I declare a JSON object with the address details.
In the next step I use the Compose – Data operation step again and add the above JSON object as a property to the initial JSON.
No wonder, in the next step we need to set this Output to the original JSON variable.
And below is the final content.
You don’t always need to use variables. You can use the JSON function with addProperty to directly add properties to JSON string. The following adds a property ZipCode to the existing Address json.
addProperty(json('{ "Address": { "City": "Milpitas", "State": "CA" } }')['Address'], 'ZipCode', '750321')
Similar to addProperty, there is removeProperty as well through which you can remove the property from JSON object.
removeProperty is quite easy to use. In our example if we want to remove the Name property from JSON, following will be our formula.
removeProperty(variables('ContentJSON'),'Name')
If we want to remove the ZipCode property of the Address object, below is the formula to do the same.
removeProperty(variables('ContentJSON')['Address'],'ZipCode')
The final function to round off is setProperty.
Below is the code to update the Name property of the JSON.
setProperty(variables('ContentJSON'),'Name', 'Maria')
Following is the expression to set ZipCode property of the Address object
setProperty(variables('ContentJSON')['Address'],'ZipCode', '112233')
In all this don’t forget to use the Set variable with output of the compose actions to reflect the changes to the original content.
Hope this helped!
You may also like the below posts.
Debajit Dutta
Business Solutions MVP
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.