0

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!!

Utsab
  • 159
  • 2
  • 4
  • 12

1 Answers1

0

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

{
    "content": [
        {
            "d": {
                "results": [
                    {
                        "username": "hury11",
                        "email": "test@mail.com"
                    },
                    {
                        "username": "hury22",
                        "email": "test@mail.com"
                    },
                    {
                        "username": "hury33",
                        "email": "test@mail.com"
                    }
                ],
                "_count": "17425",
                "_next": ""
            }
        },
        {
            "d": {
                "results": [
                    {
                        "username": "hury44",
                        "email": "test@mail.com"
                    },
                    {
                        "username": "hury55",
                        "email": "test@mail.com"
                    },
                    {
                        "username": "hury66",
                        "email": "test@mail.com"
                    }
                ],
                "_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~

Hury Shen
  • 14,948
  • 1
  • 9
  • 18