DEV Community

Cover image for Power Automate - Objects
david wyatt
david wyatt Subscriber

Posted on • Edited on

Power Automate - Objects

Everyone uses the string, boolean and integer variable, occasionally the float and array, but the object one is probably the least used. Yet I think it's the most powerful and one we all should be using.

variables

In this blog I want to cover:

  1. What is a object
  2. How to use it
  3. Working with them
  4. Benefits

1. What is a object

The object is based on json, and gives the flexibility far beyond a single type.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

The best way to think of an object is a row of an Excel table:

Field1 Field2 Field3
aValue1 aValue2 aValue3
bValue1 bValue2 bValue3
cValue1 cValue2 cValue3

So in the above table object[0] (aka row 1) would be aValue1,aValue2,aValue3, but it would look like:



{
  "Field1":"aValue1",
  "Field2":"aValue2",
  "Field3":"aValue3"
}


Enter fullscreen mode Exit fullscreen mode

The fields/columns are called 'keys' and the value stored 'value', often known as a key value pair.

What is really cool is they don't just have to be strings, so you could even have:



{
  "Field1String":"aValue1",
  "Field2Integer":2,
  "Field3Boolean":true,
  "Field4Array":["a","b","c","d"]
}


Enter fullscreen mode Exit fullscreen mode

2. How to use it

There are a couple of ways to use it, with the 3 main being

  1. Declare Config Variable
  2. Environment Variable
  3. Items

1. Declare Config Variable
Its very easy to just declare multiple variables, the issue is this uses Power Platform API calls and can make the flow hard to read/load. This is where declaring 1 or 2 object variables is very useful.

object variable

To use one of the key value pairs you just need to add the key to after the variable



variables('oTest')?['key']


Enter fullscreen mode Exit fullscreen mode

the ? allows the key to be optional, so if its missing it will return null. If you just had variables('oTest')['key'] then it would fail instead of returning null

If you really want to see the keys in the expression selector you can use a 'parseJSON', but it really isn't necessary.

parseJSON

Having the variables in one place makes the flow smaller and easier to read, additionally a object can be used to group variables together if they are related.

2. Environment Variable
Environment variables can also be set to a JSON, and so are ideal for objects too.

environment variable JSON

And these are really useful for Excel connectors, they are a pain when dealing with ALM. Because they use the 'drive' instead of library they cant use the data type environment variable. Additionally they can have a lot of fields generating lots of variables.

But you can store all of the variables in one object, making updating it a lot easier. All you need to do is peak the code and then copy all of the GUIDs over:

excel connector inputs

Environment Variable
Image description



{
  "source": "groups/633781e7-c991-4d9d-898c-2f4c466eb51c",             
  "drive": "b!r5Rbl__WfUOdD1CDghwl0dLiY14G5MRLoBDDsXLWhPOFrMB_ezQiTod5uSn88PKx",
  "file": "01IPXLVTUOG2NFWWU7QRD2CUJLAAYKIXZS",
  "scriptSource": "sites/37wcqv.sharepoint.com,dee2fa67-ddf6-4da7-8f69-602428603101,65e621ab-161c-48bd-a794-3c0455b66b42",
  "scriptDrive": "b!Z_ri3vbdp02PaWAkKGAxAash5mUcFr1Ip5Q8BFW2a0KfZsi6FcRXRI5bTXh8Brk5",
  "scriptId": "01MPL5KCEGOZTEAVNVCJDZJML3HYAQZSAV"
}


Enter fullscreen mode Exit fullscreen mode

Then to use the keys in the object you have to follow the same pattern as the initialised variable



parameters('oJSON (cr23a_oJSON)')['file']


Enter fullscreen mode Exit fullscreen mode

parameters('<Display Name> (<Name>')['key']

environment var names

3. Items
When working with arrays its easy to forget that every item in that array is actually an object, and we can use it like any other object.

set item to variable

it maybe a niche case but saves you from ever having to save multiple values in different variables when getting from a list.

3. Working with them

Because we are using the object editing them is a little more tricky then a simple variable, but we do have some expressions to help:

  1. addProperty
  2. setProperty
  3. removeProperty

1. Add property
The addProperty simply adds the key value pair to your object
addProperty(<object>, '<key>', <value>)

Because Power Automate does not allow variables to self reference (so I cant set it based on a value from itself), we have to use the compose step trick, where we compute the new value first then set it as the variable.

add property

2. Set Property
The setProperty is the most useful, as this will not only edit a propteries value is if exsists, but also create it if it doesn't (ie will act like a addProperty if the property does not exsit)
setProperty(<object>, '<key>', <value>)

2. Remove Property
Remove will delete the property/key along with the value.
removeProperty(<object>, '<key>')

Top tip, if you want to remove multiple properties then a quicker way is to use the Select acition.

Although it requires the inputs to be an array, we can simply turn our object into an array but wrapping it in [].

select on object

We can then use the first() expression to convert it back to an object.

select run log

4. Benefits

So why jump through all these extra steps and not use the simple variables. Well the main reason is efficency, as for every initialise and set of a variable a Power Platform API call is used. Each license is limited to 6/40k per day, which can add up very quickly especially with loops. Remove unnecessay actions saves those API calls. Additionally even if you think you won't ever come close to your 40k its still waste full, each API call uses energy, and if we want to be sustainable we should avoid wasting enery where ever possible.

Finally as a developer its good to use JSONs, they are now the foundation of APIs (sorry all you XML fans but you know its true ๐Ÿ˜Ž ), and the more we use them the better we are at handling them in all the actions.

Top comments (0)