Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logic App : Finding element in Json Object array (like XPath fr XML)

In my logic app, I have a JSON object (parsed from an API response) and it contains an object array.

How can I find a specific element based on attribute values... Example below where I want to find the (first) active one

{
    "MyList" : [
        {
            "Descrip" : "This is the first item",
            "IsActive" : "N"
        },
        {
            "Descrip" : "This is the second item",
            "IsActive" : "N"
        },
        {
            "Descrip" : "This is the third item",
            "IsActive" : "Y"
        }
   ]
}
like image 781
Chris Hammond Avatar asked Jul 31 '18 08:07

Chris Hammond


Video Answer


2 Answers

Well... The answer is in plain sight ... There's a FILTER ARRAY action, which works on a JSON Object (from PARSE JSON action).. couple this with an @first() expression will give the desired outcome.

enter image description here

like image 111
Chris Hammond Avatar answered Sep 28 '22 04:09

Chris Hammond


You can use the Parse JSON Task to parse your JSON and a Condition to filter for the IsActive attribute:

Use the following Schema to parse the JSON:

{
  "type": "object",
  "properties": {
    "MyList": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "Descrip": {
            "type": "string"
          },
          "IsActive": {
            "type": "string"
          }
        },
        "required": [
          "Descrip",
          "IsActive"
        ]
      }
    }
  }
}

Here how it looks like (I included the sample data you provided to test it): enter image description here

Then you can add the Condition:

enter image description here

And perform whatever action you want within the If true section.

like image 21
Martin Brandl Avatar answered Sep 28 '22 03:09

Martin Brandl