Search code examples
azure-logic-apps

How to loop and extract items from Nested Json Array in Logic Apps


I need to extract each element from nested json array. I am trying to find a way.. Schema:

{
    "id":"1",
    "name":"One",
    "child":[
        {

            "id":"2",
            "name":"two",
            "child":[]
        },
        {
            "id":"3",
            "name":"three",
            "child":[

                {
                    "id":"4",
                    "name":"four",
                    "child":[]
                },
                {
                    "id":"5",
                    "name":"five",
                    "child":[]
                }

                ]
        }

        ]

}

The resultant payload needs to be {"id":"1","name":"one"},{"id":"2","name":"two"},{"id":"3","name":"three"}


Solution

  • You might need to write a recursive function. In python, it will look like:

    def get_payload(obj):
      ret = [{'id':obj['id'], 'name':obj['name']}]
      for child in obj['child']:
        ret.extend(get_payload(child))
    
      return ret