简体   繁体   中英

Iterate array object in liquid template

I have a problem when my Liquid template iterate in array object. I want to transform an input json into another output json.

It is my Liquid Template:

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

It is an input json example:

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

When I have launched my Logic App I got this error:

{\\"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.'\\"

According to some test, it should be caused by the part shown as below in your liquid:

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

I removed this part and the liquid map works fine, here I post my liquid map below for your reference:

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

Don't worry about the last "," because the "Transform JSON to JSON" action will deal with it. In the output of the "Transform JSON to JSON" action, it will not show an extra "," at the end of the json.

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