Data Operation JSON

Bala Madhusoodhanan - May 16 '23 - - Dev Community

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

{
  "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"
        }
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

the output is an array

Image description

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"
        }
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

Image description

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'])
Enter fullscreen mode Exit fullscreen mode

Image description

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

Image description

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

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

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

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

Image description

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .