Latest Posts

How to Compose and Parse JSON in Logic Apps


In this article, we will see how to parse and use JSON in Logic apps.

Compose JSON in Logic Apps

  • Create a logic app.

1.   JSON Array with simple data types as elements

  • First, we will create a variable with values. Here, we have taken integer values in an array.
  • create array variable with value in logic apps
  • Now we will compose a JSON object using this variable
  • Create an action called Compose as shown in the screenshot below.
  • create compose action
  • Put the variable in the input of JSON.
  • put variable in compose
  • Now we are going to fetch the value of 2nd element of the JSON array object. Here, the array is 0 based index so the second element is index# 1
    Note: Since we are just composing the JSON and not parsing it, there’s no – let’s call it – IntelliSense:the dynamic content is not visible.
  • So our final formula will be: outputs('Compose')[1]
  • output compose
  • Run the flow.
  • variable value output

2.   JSON Array of objects

  • Create a Compose object and put values like those mentioned in the screenshot.
  • json array of object
  • Now we are going to add Parse JSON activity. In the Content section, you need to put the Output of the Compose activity and for schema, either you can write the schema of the Content that you just provided, or you can click on Use sample payload to generate schema and put a sample data payload so that the activity will automatically generate the schema.
  • sample payload to generate schema
  • After this step, we can utilize the output value in 2 ways.
    • First way: For Loop each object: You can loop through each object of the array. As you can see in the screenshot, you need to place the body of the previous step and all the Key/values (objects) will be available through iteration.
    • use variable output value
    • Second Way: Get a specific Record: Here, we have utilized the output of the earlier step (parse JSON), then we have picked the 2nd Object (Index# 1) and then specified the Key (color). So, it will return the color value of the second element (green) in the array.
    • set variable body parse json

3.   Single record JSON object

  • Create a Compose object and put the values as shown in the screenshot. This is a simple object having key-value pair.
    Note: Here, you can directly use the compose object output value. You don’t need to put
  • single record json compose
  • Now add parse JSON object. In the Content section, you need to put the Output of the Compose activity and for schema, either you can write the schema of the Content that you just provided, or you can click on Use sample payload to generate schema and put a sample data payload so that the activity will automatically generate the schema.
    Note: Here, as the JSON is in a simple format, you don’t need to use Parse JSON. You can directly use the output of the compose object in your variable and it will behave the same way.
  • single record json output
  • If you want to get a specific value, then you can achieve using this expression.
    • If you want to get value using Compose object,
      outputs('Compose')[1]
    • If you want to get value using Parse JSON action.
      body('ParseJSON')['color']

4.   Highly nested and complicated JSON object

  • we are going to use the below JSON. Put that in JSON compose.

{

    "id": "0001",

    "type": "donut",

    "name": "Cake",

    "ppu": 0.55,

    "batters":

         {

                "batter":

                     [

                           { "id": "1001", "type": "Regular" },

                           { "id": "1002", "type": "Chocolate" },

                           { "id": "1003", "type": "Blueberry" },

                           { "id": "1004", "type": "Devil's Food" }

                     ]

         },

    "topping":

         [

               { "id": "5001", "type": "None" },

               { "id": "5002", "type": "Glazed" },

               { "id": "5005", "type": "Sugar" },

               { "id": "5007", "type": "Powdered Sugar" },

               { "id": "5006", "type": "Chocolate with Sprinkles" },

               { "id": "5003", "type": "Chocolate" },

               { "id": "5004", "type": "Maple" }

         ]

}

  • Now, you can put the Parse JSON object. Now the main question is how to use each section/item/value from this object. We will see that about each section from the screenshot.
  • Highly nested and complicated JSON object
  • Section A: In the variable or wherever you are using the value, you just need to directly call that attribute. E.g. if you want the value of ppu (0.55), you can write it like this.
  • complicated json set variableA
  • Section B: This section values have got 2 levels of hierarchy. Batters >> batter >> id/type. Here we are targeting the third (Index# 2) value of the id attribute (1003).
  • You just need to keep specifying the attributes hierarchy-wise as shown below and wherever the index is there, don’t specify the quotes.
    body('ParseJSON5')['batters']['batter'][2]['id']

  • complicated json set variableB
  • Section C: Get 5th value (index# 4) value of type attribute. Like the above example, we will have to put an expression here.
    body('ParseJSON5')['topping'][4]['type']

5.   Array of Highly nested and complicated JSON object

  • We are going to see an array of the complicated JSON object
  • Array of Highly nested and complicated JSON object
  • Section A: we are going to see the value from section A2, name attribute (Raised).
    body('ParseJSON6')[1]['name']
  • Section B: Section B2 1st (Index# 0) value of Id (1001)
    body('ParseJSON6')[1]['batters']['batter'][0]['id']
  • Section C: Section C1, 3rd row value of attribute type (Sugar)
    body('ParseJSON6')[0][‘topping’][2]['type']

We value your Feedback:

Page URL:

Name:

Email:


Suggestion:

© 2024 Code SharePoint