简体   繁体   中英

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

我有这个 json,我正在尝试使用液体模板将其解析为另一个 json。我不知道如何遍历元素。

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

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": ""
            }
        }
    ]
}

在此处输入图像描述

Then parse json and use "Transform JSON to JSON" action. 在此处输入图像描述

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~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM