Internally Microsoft Flow (or Logic Apps) loads workflow objects in JSON format.
I wrote this post for my self as a quick memo of how to use JSON in Microsoft Flow and I hope that it will be useful to you too.
Let’s create a very basic flow hosting a list of my favorite Formula 1 pilots stored in a JSON array. Of course this list of objects can come from somewhere else like a REST Service…
- First create a Button Flow and define the input like this:
- Add a compose action of type Object.
- Name the variable ‘Pilots’.
- Paste the following JSON object:
“Name”: “Jan manual”,
Now the question is how can I find for instance the first pilot from in my Flow ?
- Add a Compose action, name it First; go the the Expression tab and add the following expression:
variables(‘Pilots’) returns the Pilot variable.
variables(‘Pilots’)[‘People’] returns the people array
variables(‘Pilots’)[‘People’] returns the first pilot
variables(‘Pilots’)[‘People’][‘Surname’] returns the first pilot surname. Easy.
Now let say, we want to retrieve the Name of the pilot when we submit the Surname in the input button. We will have to loop through all items.
- Add a Apply to each action.
- And in the Apply to each add the following expression: variables(‘Pilots’)[‘People’]:
We can always retrieve the current item in a Apply to each with the item() expression. Here we just need to point to the People array.
We need to check if the current item is the pilot we are looking for :
- let’s add a condition with the following expression :
- let’s compare the Surname to the one provided in the input button:
- If it matches, then the flow can send an e-mail with the Pilot name :
if we manually run the flow :
We get the following e-mail :
Another case is when we use the SharePoint GetItems action usually followed by an Apply to each action; the objects fetched from SharePoint Get items are conveniently also represented as JSON.
Here I have a list of offices in a SharePoint list
If you use a GetItems followed by an Apply to each check each item, the apply to each needs the value property of the GetItems action; if you want to check the Capacity column of each element, you will also have to use the syntax item()[‘Capacity’]
But this is not necessary, we can also use the Capacity property in the Dynamic content like this :
I encourage you to try the tutorial published by the Flow team here.
Now there is another way to deal with JSON : use the Compose Parse JSON action, define a schema and you won’t have to parse your JSON anymore :
In this Parse JSON action, just specify the JSON content you want to parse (here it is the Pilots variable), and provide a schema (our JSON data can be use to build the schema)
To build the schema just click on the link “Use sample payload to generate schema” copy the pilots JSON data and paste them in the Shema; you should get something like this :
Everything should be easier now; if you add an apply for each action:
As you can see, you don’t have to rely on expression to grab the values, you can rely on the Dynamic content, which is easier 🙂