2

I have a Parse JSON action with following schema:

{
    "type": "object",
    "properties": {
        "employees": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "email": {
                        "type": "string"
                    }
                },
                "required": [
                    "id",
                    "email"
                ]
            }
        }
    }
}

And the sample input JSON string is:

{
  "employees": [
    {
      "id": "1111",
      "email": "email1@gmail.com"
    },
    {
      "id": "2222",
      "email": "email2@gmail.com"
    }
  ]
}

To get employee list, I use:

body('Parse_JSON')?['employees']

How get the email of emloyee with id = 2222?

Note: I can change JSON schema if I have to.

NoName
  • 7,940
  • 13
  • 56
  • 108
  • 1
    body('Parse_JSON')?['employees'][1]['id'] , is'nt this working ? – HariHaran Jan 02 '19 at 08:56
  • @HariHaran it is not always at the second position, that is the point. I need a good way to get an array element with specific property value. Loop throug entire array is an option but it is not quite good, especially with MS Flow. – NoName Jan 02 '19 at 09:58
  • Is that a requirement that you need the id in the flow itself ? Can you have a look at this if it helps https://blog.mexia.com.au/transforming-json-objects-in-logic-apps – HariHaran Jan 02 '19 at 10:01

2 Answers2

3

I came across your question a year later and there still isn't a good way of doing it. I ended up using a Filter Array step (under Data Operations)

Filter Array

You can then apply the filter conditions here. e.g. id = 1111

Apply filter

This still returns an array, but then I can use a Compose operation to get the first() element of the array. I also take this opportunity to use coalesce() to set a default value if the value is not found:

coalesce(first(body('Filter_array')).value, 10000)

jasonscript
  • 6,039
  • 3
  • 28
  • 43
-2

Loop over the array and "Filter array" to leave only the array item that matches the 'id' = 2222. Should have only one item if the list was unique. If multiple entries, filter on some other value.

Create a variable.

Then, look loop over the filtered array and set the variable to whatever is in the array items. Be aware, the last item wins. Could cause bugs if items filtered are non-unique and non-identical.