简体   繁体   中英

Properly formatting an array in a liquid template for a JSON object

I am trying to create a JSON object using a liquid template, but the fields array in my output is not properly formatted. For example, my input is:

{
    "queryString": "id:00000000-0000-0000-0000-000000000000",
    "fields": [
        "linkFilename",
        "documenttype",
        "description",
        "webUrl"
    ]
}

And my desired output is:

{
    "requests": [
        {
            "entityTypes": [
                "listItem"
            ],
            "query": {
                "queryString": "id:00000000-0000-0000-0000-000000000000"
            },
            "region": "EMEA",
            "fields": [
                "linkFilename",
                "documenttype",
                "description",
                "webUrl"
            ]
        }
    ]
}

But my current liquid template:

{% capture output %}
{
    "requests": [
        {
            "entityTypes": ["listItem"], 
            "query": { 
                "queryString": "{{ queryString }}" 
            }, 
            "region": "EMEA", 
            "fields": ["{{ fields }}"]
        }
    ]
}
{% endcapture %}
{{ output }}

results in:

{
    "requests": [
        {
            "entityTypes": [
                "listItem"
            ],
            "query": {
                "queryString": "id:00000000-0000-0000-0000-000000000000"
            },
            "region": "EMEA",
            "fields": [
                "linkFilenamedocumenttypedescriptionwebUrl"
            ]
        }
    ]
}

How can I separate the elements in the fields array with a comma in the liquid template?"

{% capture fieldList %}
    {% for i in input.fields %}
        "{{ i }}"
        {% if forloop.last != true %},{% endif %}
    {% endfor %}
{% endcapture %}
{% capture output %}
{
    "requests": [
        {
            "entityTypes": [
                "listItem"
            ],
            "query": {
                "queryString": "{{ input.queryString }}"
            },
            "region": "EMEA",
            "fields": [
                {{ fieldList }}
            ]
        }
    ]
}
{% endcapture %}
{{ output }}

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