在液体模板中迭代数组对象

Iterate array object in liquid template

我的 Liquid 模板在数组对象中迭代时遇到问题。我想将输入 json 转换为另一个输出 json.

这是我的液态模板:

{% assign clientList = Clients.value %}

{
    "value": [
        {% for client in clientList %}
            {
                "ACCOUNTNUM": "{{client.ACCOUNTNUM}}",
                "EMAIL": "{{client.EMAIL}}",
                "NAME": "{{client.NAME}}",
                "PHONE": "{{client.PHONE}}",
                "VATNUM": "{{client.VATNUM}}",
                "RECID": "{{client.RECID}}",
                "CANALID": "{{client.CANALID}}",
                "CANALDESC": "{{client.CANALDESC}}"
            }
                {% if forloop.Last == false %}
                , 
                {% endif %}
            {% endfor %}
    ]
}

它是一个输入 json 示例:

{
   "Clients":{
      "value":[
         {
            "@odata.etag":"",
            "ItemInternalId":"3a93f2aa-dd77-4297-88c4-13241343321",
            "ACCOUNTNUM":"01",
            "EMAIL":"info@example.es",
            "LANGUAGEID":"es",
            "NAME":"Lili S.A.",
            "PHONE":"943444666",
            "VATNUM":"A01",
            "DATAAREAID":"tr2",
            "RECID":1412,
            "DATAAREAID_x0023_2":"tr2",
            "DATAAREAID_x0023_3":"tr2",
            "CANALID":"C0010",
            "CANALDESC":"Group gg"
         },
         {
            "@odata.etag":"",
            "ItemInternalId":"3a23f2aa-dd77-4297-88c4-13241343321",
            "ACCOUNTNUM":"02",
            "EMAIL":"info@example.es",
            "LANGUAGEID":"es",
            "NAME":"Lili2 S.A.",
            "PHONE":"943444656",
            "VATNUM":"A02",
            "DATAAREAID":"tr2",
            "RECID":1412,
            "DATAAREAID_x0023_2":"tr2",
            "DATAAREAID_x0023_3":"tr2",
            "CANALID":"C0011",
            "CANALDESC":"Group2 gg"
         }
      ]
   }
}

当我启动我的 Logic App 时,出现了这个错误:

{\"StatusCode\":400,\"ErrorCode\":18,\"Details\":null,\"Message\":\"An error occurred while converting the transformed value to JSON. The transformed value is not a valid JSON. 'After parsing a value an unexpected character was encountered: :. Path 'print1', line 5, position 11.'\"

根据一些测试,应该是你的液体中有如下所示的部分引起的:

{% if forloop.Last == false %}
, 
{% endif %}

我删除了这部分并且液体贴图工作正常,这里我post下面我的液体贴图供您参考:

{
    "value": [
        {% for client in content.Clients.value %}
            {
                "ACCOUNTNUM": "{{client.ACCOUNTNUM}}",
                "EMAIL": "{{client.EMAIL}}",
                "NAME": "{{client.NAME}}",
                "PHONE": "{{client.PHONE}}",
                "VATNUM": "{{client.VATNUM}}",
                "RECID": "{{client.RECID}}",
                "CANALID": "{{client.CANALID}}",
                "CANALDESC": "{{client.CANALDESC}}"
            }, 
        {% endfor %}
    ]
}

不要担心最后的“,”,因为 "Transform JSON to JSON" 操作会处理它。在 "Transform JSON to JSON" 操作的输出中,它不会在 json.

的末尾显示额外的“,”