Jegan Raj
Jegan Raj

Reputation: 125

How to Transform from xml to json in Azure Logic apps using Liquid

I am using the Transform XML to JSON. This is my request body in XML

<Root>
<test>
    <column1>value1</column1>
    <column2>value2</column2>
</test>
<test>
    <column1>value1</column1>
    <column2>value2</column2>
</test>
</Root>

and this is my liquid map

{
    "test": [
        {% for data in Root.test %}
        {
            "column1": "{{data.column1}}",
            "column2": "{{data.column2}}",
        }
        {% endfor %}
    ]
}

while running the logic app i am getting parsing error:

{
  "Code": "IncorrectLiquidTransformOutputType",
  "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 'test[0]'"
}

But while I run the same code in the liquid sandbox is working fine. may I know? what is the issue here with logic apps.

Upvotes: 0

Views: 9647

Answers (3)

Rakesh Suryawanshi
Rakesh Suryawanshi

Reputation: 520

You should use if condition to add "," {% if forloop.last == false %},{% endif %}

{
"test": [
    {% for data in content.Root %}
    {
        "column1": "{{data.column1}}",
        "column2": "{{data.column2}}"
    }{% if forloop.last == false %},{% endif %}
    {% endfor %}
]}

Upvotes: 0

Chris Halcrow
Chris Halcrow

Reputation: 31940

I'll summarise the answers/comments already provided as I didn't see the issue when I first checked the accepted answer!

This type of error can be caused by missing out a comma at the end of a JSON object literal that you want to be repeated as elements in the array, in the transformed output.

Fails

[
    {% for data in Root.test %}
    {
        "column1": "{{data.column1}}",
        "column2": "{{data.column2}}",
    }
    {% endfor %}
]

Works

[
    {% for data in Root.test %}
    {
        "column1": "{{data.column1}}",
        "column2": "{{data.column2}}",
    },
    {% endfor %}
]

Upvotes: 0

Hury Shen
Hury Shen

Reputation: 15724

I test it in my side and provide my liquid map below for your reference:

{
    "test": [
        {% for data in content.Root %}
        {
            "column1": "{{data.column1}}",
            "column2": "{{data.column2}}"
        },
        {% endfor %}
    ]
}

Run this liquid map and it works fine(shown as below screenshot) enter image description here

Hope it helps~

Upvotes: 4

Related Questions