In Azure Logic apps, what is/are the advantage(s) of using a liquid template to convert xml?

278 Views Asked by At

In logic apps you can simply call json(xmlStringHere) to convert your xml to json, so not sure what do we need liquid template for.

1

There are 1 best solutions below

0
On

Adding to what @Skin mentioned, Sure, you can directly convert XML to JSON. However, in a few cases if the data is too complex or large to handle Liquid template comes into play. Liquid templates offer the schema too along with the conversion which maps the source data to required JSON format. To be more clear below is a sample for demonstration purposes.

Below is the XML that I'm trying to map through Liquid template

<Customer>
    <Name>aaa</Name>
    <PhoneNumber>1234567890</PhoneNumber>
    <Locations>
        <Location1>Location1</Location1>
        <Location2>Location2</Location2>
    </Locations>
    <Orders>
        <Order>
            <OrderId>12345</OrderId>
            <NoOfItems>2</NoOfItems>
        </Order>
        <Order>
            <OrderId>12346</OrderId>
            <NoOfItems>5</NoOfItems>
        </Order>
    </Orders>
</Customer>

Here is the template that I'm looking for where I can retrieve the required fields which looks a bit more complex when we try to build the same using logic apps alone.

{
    "Name" : "{{content.Customer.Name}}",
    "PhoneNumber" : "{{content.Customer.PhoneNumber}}",
    "Locations": "{{ content.Customer.Locations.Location1 %}}",
    "Orders": [
        {% for order in content.Customer.Orders %}
            {
            "OrderId" : "{{order.Order.OrderId}}"
        },
      {% endfor %}
    ]
}

Below is the flow of my Logic app

enter image description here

Results

enter image description here

For more information you can refer Convert JSON and XML with Liquid templates