Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.


Getting started with Logic App : Liquid Map | Using Liquid template in Logic app

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>iPhoneproductname>
        productid>EL123productid>
        productcategory>Electronicsproductcategory>
    Product>
    Product>
        productname>OnePlusproductname>
        productid>EL124productid>
        productcategory>Electronicsproductcategory>
    Product>
    Product>
        productname>Xioamiproductname>
        productid>EL125productid>
        productcategory>Electronicsproductcategory>
    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.



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. 



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.



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





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


This post first appeared on TechFindings, please read the originial post: here

Share the post

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

×

Subscribe to Techfindings

Get updates delivered right to your inbox!

Thank you for your subscription

×