Apologies for the delay. It’s been a rough few weeks lately.

A lot of the work we do as developers is to pass data back and forth. Typically these days that data is passed in JSON format (though the usage of XML is still fairly widespread). Sometimes we need to make changes to that data as it passes through. Thankfully, Power Automate provides a few functions to help you work with JSON data.

addProperty

The addProperty function is used to add a new property to a JSON object. At its base level, it adds that property to the top level of the JSON object.

 addProperty(<object>, <property>, <value>)

represents the JSON object you are adding the property to. is the name of the property to be added and is the value.

The output value is the updated JSON object with the new property added to it. It’s important to remember that in this simplest form, it can only add properties at the top level of the object.

addProperty({'id':1}, 'name', 'Frank')
// returns update object
// { 'id':1, 'name':'Frank'}

If the property already exists, then this function will throw an error. If the property already exists, the setProperty function must be used (see below).

If you need to add the property at a deeper level, then this function needs to be combined with the setProperty function, as follows:

setProperty(<object>, <parent-property>, addproperty(<object>[<parent-property>], <property>, <value>)

So this will get a bit complicated. As before, the is the whole object. is the parent property within the JSON object that you are adding a new property to. and are the new values, same as before.

Let’s say we have the following JSON object stored in a variable called ‘marvel’:

{
  "id": 1,
  "name": {
    "first":"Marc",
    "last":"Spector"
  }
}

And we want to add a middle name value:

setProperty(variables('marvel'), 'name', addProperty(variables('marvel')['name'], 'middle', 'Moon Knight'))

What this essentially does is take the subset name property, add the middle name to it and then replace it in the original object with the updated version. We get the output as follows:

{
  "id": 1,
  "name": {
    "first": "Marc",
    "last": "Spector",
    "middle": "Moon Knight"
  }
}

removeProperty

This function works similarly to the addProperty function on JSON objects, except that it removes a property. There are two formats. The first is used to remove properties at the top level:

removeProperty(<object>, <property>)

The parameters work the same as those for addProperty.

The second format is used if you need to remove a child property deeper in the JSON object.

The output result is the updated JSON object.

removeProperty(<object>, <parent-property>, <property>)

Let’s consider our Marc Spector JSON object from before:

removeProperty(variables('marvel'), 'name')  // This would remove the entire name property, including first, middle and last
removeProperty(variables('marvel'), 'name', 'middle')  // This would remove the middle property from name, leaving just first and last

If you try to remove a property that doesn’t exist, no errors will occur and the response will just be the original JSON object.

setProperty

This function will allow you to update an existing property in a JSON object. The format is the same as addProperty with different formats for the top level and sub-properties.

setProperty(<object>, <property>, <value>)
setProperty(<object>, <parent-property>, setProperty(<object>[parent-property], <property>, <value>))

The output is the updated original JSON object. It’s important to note here, that if the property does not exist, it will not get added. You will just get the original JSON object returned in an unchanged state.

Conclusion

While it isn’t the easiest way of working with JSON data, Power Automate does provide use more than a little help to do what needs to be done. Next time, I’ll take a look at searching XML data in Power Automate using XPath.