Parsing JSON data within Logic Apps

Parsing JSON data

This post contains samples on how to parse JSON data within Azure Logic workflows. In addition, I’m including some information on how to consume the parsed data by implementing a simple iteration over a JSON object array.

Parsing JSON data – @Json()

There will be times when you have to deal with JSON data returned as text. Luckily the templating language has a build-in function called @JSON() which will do all the parsing magic for you. Instead of pointing you to the manual, I will explain how this works using a sample.

Note: Please be aware that the sample below apply to the 2015-08-01-preview version. In the prior schema version, the function for parsing JSON data is called @Parse().

I’m using a test data API App which is responsible for returning the JSON data as text/plain as shown below. Generic.JsonString represents my Json data.

And returns the data as shown below.

"{\"employees\":[{\"firstName\":\"John\", \"lastName\":\"Doe\"},{\"firstName\":\"Anna\", \"lastName\":\"Smith\"}, {\"firstName\":\"Peter\", \"lastName\":\"Jones\"}]}"

Workflow layout

My Logic App workflow is very simple;

1 – Manual Action > 2 – Web Service call “Values_GetJsonString” > 3 – HTTP Action iterating over all employees, posting the first name only. The interesting bits are all controlled within the final HTTP Action.

Parsing-JSON-data

First of all, the action needs to consume the data parsed as JSON: @Json(body('Values_GetJsonString')) However, I would like to iterate over all employees which would basically look like this: @Json(body('Values_GetJsonString')).employees.

At this point it’s possible to access the data for each item in the collection using the @item() function.
Result: @item()['firstName']

Note: As soon as you save and re-open the workflow in code view, you will notice that the @item() has been replaced with @triggerBody(). I believe this is due to the way Logic Apps is evaluating expressions.

Resources

Complete code sample: Parsing JSON data

Related articles:

https://msdn.microsoft.com/en-us/library/azure/mt643789.aspx
http://stackoverflow.com/questions/32380905/parsing-json-in-azure-logic-app

Post Navigation