Logic App - Xml to Json using Liquid Map | Append in Liquid Map

{tocify} $title={Table of Contents}


Introduction

Logic app does support transformation using Liquid Map.  Using Liquid map we can transform json-json, xml-json , json-csv etc.

Fundamentals of Liquid map with Json-Json example is covered in following post-


In this post, let's see how to transform xml to json and also how to use Append and Plus function in Liquid map 


Scenario


Consider you will receive details of multiple product/item in xml format over the http request and you need convert/transform it in json format.

Also you need to create list of product/item and count of product/item.

And return it as http response.

Input



<?xml version="1.0" encoding="UTF-8"?>
<Products>
    <Product>
        <productname>iPhone</productname>
        <productid>EL123</productid>
        <productcategory>Electronics</productcategory>
    </Product>
    <Product>
        <productname>OnePlus</productname>
        <productid>EL124</productid>
        <productcategory>Electronics</productcategory>
    </Product>
    <Product>
        <productname>Xioami</productname>
        <productid>EL125</productid>
        <productcategory>Electronics</productcategory>
    </Product>
</Products>



Map


The below liquid map takes above xml as input, loops through each product in it and creates product in json format.

While doing so it assigns  productname to listofitems variable and 1 to itemcount variable for first iteration, then after productname is appended with comma in between and itemcount is incremented by 1 for the rest of iteration.

In each iteration it checks if it is the last, if not then comma is added.

{
         "Products":
[
{% assign listofitems = "" %}
{% assign itemCount = 0 %}
{% for item in content.Products %}
    {% if forloop.first == true %}
   
         {% assign listofitems =  item.productname %}
   {% assign itemCount = itemCount | Plus: 1 %}
                    {% else %}
              
       {% assign listofitems =  listofitems | Append: ',' | Append: item.productname %}
       {% assign itemCount = itemCount | Plus: 1 %}

   {% endif %}
     "name":"{{item.productname}}",
"id":"{{item.productid}}",
"category":"{{item.productcategory}}"
}

 {% if forloop.last == false %},{% endif %}
{% endfor %}
 
],
"ProductsSummary":
     "listofitems":"{{listofitems}}",
 "itemCount":"{{itemCount}}"
}
}

Save the map with .liquid extension.


Upload the liquid map to Integration Account


In order to use the above created liquid map, it has to be uploaded in Integration Account.

Go to Integration account, select Maps from left pane then click on +Add button and upload the map, specify name and type.

Upload Liquid map to Integration account


Create a Logic App workflow 


In the Azure search box, enter logic apps, and select Logic apps.On the Logic apps page, select Add.

Provide the basic information as Subscriptions, resource group,region, logic app name and select consumption plan.

Click on Review and create, once created - go to resource and you will be presented with templates to choose from.

Select the Blank Logic app template, the designer shows an empty workflow.

First step here would be to link the workflow with the Integration Account. 

Link Integration account with Logic app workflow


Doing so will make the map created above accessible to the workflow.

Add Http based trigger, followed by Transform XML to JSON action
to which against Content provide triggerbody as value, against Map select the map(productxml2json) which you uploaded in Integration account.


XML to JSON transform using Liquid Map

Finally add Response action with Body as output of above transform action.

That's it, workflow is ready to use.



Testing


After you save the workflow, copy the url .

Now using any rest client like postman, ARC etc test it by providing the input as below

Input


testing input



Output

testing output



Summary

As can be seen from output, XML input was transformed to JSON and we saw how to use Append and Plus function in Liquid map

{
  "Products": [
    {
      "name""iPhone",
      "id""EL123",
      "category""Electronics"
    },
    {
      "name""OnePlus",
      "id""EL124",
      "category""Electronics"
    },
    {
      "name""Xioami",
      "id""EL125",
      "category""Electronics"
    }
  ],
  "ProductsSummary": {
    "listofitems""iPhone,OnePlus,Xioami",
    "itemCount""3"
  }
}



Learn More about Logic App

Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment

Previous Post Next Post