Power Automate - Objects
Tags: powerautomate, powerplatform, json, rpa
Published: 2024-04-08 05:18:05

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4yhceyclwrqosf78u97.png)

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](https://www.json.org/json-en.html) (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:

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

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:

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

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nw5hnty0abul6g9yomcw.png)

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

```json
variables('oTest')?['key']
```
_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysgwwyxh0u8vbon9rwmf.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bj2q78019ebhby91nzgx.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq4rsqr4j9zyaaqoa4x5.png)

Environment Variable
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qxima0ldvhyuh22t76qz.png)

```json
{
  "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"
}
```

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

```
parameters('oJSON (cr23a_oJSON)')['file']
```
`parameters('<Display Name> (<Name>')['key']`

![environment var names](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7gqa697xq02xsi1c86b.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwzwq2lg1do2e57i28sb.png)

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](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#addproperty)
2. [setProperty](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#setproperty)
3. [removeProperty](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference#setproperty)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5tv3jl9usp6wdoosdms.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16g21psgo9qdvvczyh2k.png)

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

![select run log](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6huxkt9g58bwb0wzbv51.png)

## 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 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 wont 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 whereever 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.