Search code examples
jsonparsingazure-logic-appsdotliquidliquid-template

Parsing JSON to JSON in Liquid Template using for loop. How to iterate through lists in JSON using Liquid Template?


I have this json that i am trying to parse into another json using liquid template. I don't know how to loop through elements.

{
"SAP": [
    {% for Record in content %}{
        {% assign res = Record.d['results'] %}      
            "SSO ID": "{{ res[0].username }}"   
    },
    {% endfor %}
    ]
}

Using this - i am only able to get username field of the first element from both results but not the second element. I want to be able to iterate through all the elements both results and get their values..

PLEASE HELP!!


Solution

  • For our requirement, I initialize a variable named "data" and store the below value to simulate your situation.

    {
        "content": [
            {
                "d": {
                    "results": [
                        {
                            "username": "hury11",
                            "email": "[email protected]"
                        },
                        {
                            "username": "hury22",
                            "email": "[email protected]"
                        },
                        {
                            "username": "hury33",
                            "email": "[email protected]"
                        }
                    ],
                    "_count": "17425",
                    "_next": ""
                }
            },
            {
                "d": {
                    "results": [
                        {
                            "username": "hury44",
                            "email": "[email protected]"
                        },
                        {
                            "username": "hury55",
                            "email": "[email protected]"
                        },
                        {
                            "username": "hury66",
                            "email": "[email protected]"
                        }
                    ],
                    "_count": "17425",
                    "_next": ""
                }
            }
        ]
    }
    

    enter image description here

    Then parse json and use "Transform JSON to JSON" action. enter image description here

    My liquid template shown as below:

    {
        "SAP": [
            {% for Record in content %}
               [
               {% for result in Record.d.results %}
                  {
                    "SSO ID": "{{result.username}}",
                    "email": "{{result.email}}"
                  },
               {% endfor %}
               ],
            {% endfor %}
        ]
    }
    

    If you want the d map in your result json data, the liquid template should be as below:

    {
        "SAP": [
            {% for Record in content %}
            {"d":
               [
               {% for result in Record.d.results %}
                  {
                    "SSO ID": "{{result.username}}",
                    "email": "{{result.email}}"
                  },
               {% endfor %}
               ]
            },
            {% endfor %}
        ]
    }
    

    Hope it helps~