繁体   English   中英

在 JSON object 的液体模板中正确格式化数组

[英]Properly formatting an array in a liquid template for a JSON object

我正在尝试使用液体模板创建 JSON object,但我的 output 中的字段数组格式不正确。 比如我的输入是:

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

我想要的 output 是:

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

但是我目前的液体模板:

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

结果是:

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

如何在液体模板中用逗号分隔字段数组中的元素?”

{% 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 }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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