Data Operation JSON
Tags: powerautomate, powerfuldevs, json
Published: 2023-05-16 06:55:27

JSON (JavaScript Object Notation) are widely used lightweight data-interchange format for representing structured data and exchanging information between different system. 

We will use Primary and Secondary colour dataset to perform CRU operations with JSON

1. **Create a JSON data**

**Method 1**: Leverage the compose to create JSON in the power automate flow. The intelligence of the compose function to associate the data type with respect to the input data is a lovely pre-built feature


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



```
{
  "Pallet": {
    "Pcolors": [
      {
        "ID": "ID01",
        "color": "red",
        "category": "hue",
        "type": "primary",
        "code": {
          "rgba": [
            255,
            0,
            0,
            1
          ],
          "hex": "#FF0"
        }
      },
      {
        "ID": "ID02",
        "color": "blue",
        "category": "hue",
        "type": "primary",
        "code": {
          "rgba": [
            0,
            0,
            255,
            1
          ],
          "hex": "#00F"
        }
      },
      {
        "ID": "ID03",
        "color": "yellow",
        "category": "hue",
        "type": "primary",
        "code": {
          "rgba": [
            255,
            255,
            0,
            1
          ],
          "hex": "#FF0"
        }
      }
    ]
  }
}
```
the output is an array 


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


**Method 2**: Leverage the Compose function and use the datatype as object and provide the input

```
{
  "Pallet": {
    "Scolors": [
      {
        "ID": "SID01",
        "color": "green",
        "category": "hue",
        "type": "secondary",
        "code": {
          "rgba": [
            0,
            255,
            0,
            1
          ],
          "hex": "#0F0"
        }
      },
      {
        "ID": "SID02",
        "color": "orange",
        "category": "hue",
        "type": "secondary",
        "code": {
          "rgba": [
            255,
            165,
            0,
            1
          ],
          "hex": "#ffa500"
        }
      }
    ]
  }
}
```


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

2.**Merging JSON** 
To combine two arrays or collections of values into a single array, removing any duplicates in the process we would leverage union function. The ['Pallet'] part is used to access a specific property of the from the output object (PrimCols and SecCols)

```
union(outputs('PrimCols')?['Pallet'],variables('SecCols')?['Pallet'])
```


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




3.**Navigating JSON**:
The navigation is done by the ?[‘propertyName’] string added to the source reference expression.


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


`outputs('Merge')?['Pcolors']
`

4.**Sorting JSON**:
The command outputs('Merge')?['Pcolors'] retrieves the value of the 'Pcolors' property from the output of the 'Merge' action fro step 2. **_sort_** leverages specific attribute **_color _** to sort the array

`sort(outputs('Merge')?['Pcolors'],'color')
`

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

5.**Navigating JSON**
The first part of code retrieves the value of the 'Pcolors' property. The index **_[0]_** represents the first element in the array. **_?['code']_**: Retrieves the value of the 'code' property from the selected object. The ? operator is used for optional chaining, which ensures that the expression doesn't throw an error if any intermediate property is null or undefined.

`outputs('Merge')?['Pcolors'][0]?['code']`


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

6.**Count Objects**:
The expression the length function calculates the number of objects in the array.

`length(outputs('Merge')?['Scolors'])`

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



By leveraging **JSON** in Power Automate, you can effectively handle data transformation, integrate with APIs, extract relevant information, and perform custom data operations, enhancing the capabilities and flexibility of your workflows.