Using JSON in Microsoft Flow (Part 1)

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:

pilots1pilots2

  • Add a compose action of type Object.
  • Name the variable ‘Pilots’.
  • Paste the following JSON object:

 

{

{
“people”: [

{

“Name”: “Jim”,
“Surname”: “Clark”

},

{

“Name”: “Jan manual”,
“Surname”: “Fangio”

},

{
“Name”: “Ayrton”,
“Surname”: “Senna”
}

]

}

 

pilots2

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:

pilots3

variables(‘Pilots’) returns the Pilot variable.

variables(‘Pilots’)[‘People’] returns the people array

variables(‘Pilots’)[‘People’][0] returns the first pilot

variables(‘Pilots’)[‘People’][0][‘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’]:

pilots9

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 :

pilots10

  • let’s compare the Surname to the one provided in the input button:

pilots6

  • If it matches, then the flow can send an e-mail with the Pilot name :

pilots11

 

if we manually run the flow :

 

pilots11

We get the following e-mail :

pilots12

 

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

offices

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’]

offices2

 

But this is not necessary, we can also use the Capacity property in the Dynamic content like this :

blitch5

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 :

offices3

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)

offices4

 

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 :

offices6

Everything should be easier now; if you add an apply for each action:

offices7

offices8

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 🙂

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s