JSON (JavaScript Object Notation) is a lightweight data interchange format commonly used for storing and transmitting data between a server and a web application. It provides a simple and structured way to represent data in key-value pairs, making it easy to parse and manipulate in various programming languages.
Power Automate offers several options for parsing JSON data:
- Parse JSON action: This built-in action allows you to extract values from a JSON object by providing a JSON schema. It automatically generates dynamic outputs for the parsed properties.
- Using expressions: Power Automate supports using expressions to extract specific values from JSON. You can utilize functions like ‘json’ and ‘xpath’ to access nested properties or arrays within the JSON object.
- Parse JSON custom connector: If you are working with a custom connector, you can define a JSON schema within the connector to parse incoming JSON data automatically.
When working with JSON, the Parse JSON function is effective if you are familiar with the JSON’s schema structure. However, when the JSON has a dynamic structure, parsing it is still possible, but accessing specific values becomes more challenging. To extract a value, you usually need to know the corresponding key. If the key is dynamic and unknown, it can be difficult to retrieve the desired value from the JSON.
If xpath is used it is quite like the regex it will parse the JSON and get us the key and value from the JSON. This will help us to then use the Dynamic key and get the value from it.
For e.g. if your JSON looks something like this,
{
"results": {
"key1":"value1",
"key2":"value2",
"key3":"value3",
"key4":"value4",
"key5":"value5"
}
}
The below expression can be used to retrieve all the keys and values in an array. It can then be applied with the Apply to Each control and looped through to obtain the key and value. Just replace the <JSON String> with your JSON.
xpath(xml(<JSON String>), '/results/*')
Expression to get the key is below.
xpath(item(), 'name(/*)')
Expression to get the values is below.
xpath(item(), 'string(/*)')
Another way to obtain the dynamics key is by utilizing the compose method and employing a configuration or similar setup to search for the available keys using the provided option. In the below expression, the outputs(‘key’) is a dynamic key string that can be anything. One drawback with this approach is that it will fail if the key is not present which needs to be handled graciously.
outputs('Json_Payload')[outputs('Key')]
