Convert XML to XML using a Liquid Template in Azure 😀

Convert XML to XML using a Liquid Template in Azure 😀

Hi there 😀! Today, let’s dive into an intriguing topic: how to convert XML to XML using a Liquid Template in Azure. After searching across the internet for solutions on XML to XML transformation in Azure, I discovered that Azure provides only below Liquid templates , those are :

I was pondering how to achieve XML-to-XML transformation, and finally, I discovered a solution! 😄
Let’s explore it together:

Prerequisites :

  1. Azure subscription
  2. Create Logic App

https://rit-17.medium.com/lets-create-together-logic-app-c3e35f02a8a5

Step1 : To begin, I crafted a sample XML dataset along with a corresponding Liquid template.

XML file :

<?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>

Liquid File :

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

Step 2 : To include the Liquid Map file in the Artifacts, simply navigate to the ‘Maps’ section as illustrated below 👇🏻 and upload your Liquid file.

Step 3 : Now , let’s design our Workflow:

Add Http trigger

In the action step, search for ‘Liquid’ to explore various mapping conversions. In this example, I’ve chosen to convert XML to JSON.

All the necessary details have been successfully filled in for the Liquid operation transforming XML to JSON :

Content : dinamically select Body of Http trigger.

Source : I am selecting here Logic App as I have added my Liquid Map file in my Logic app’s Artifacts section.

Since the previous action generates the transformed content in JSON format, but I require it in XML, I have chosen the ‘Execute JavaScript Code’ action to achieve this.

Here, I’ve crafted a JavaScript code snippet designed to seamlessly transform JSON data into XML format:

function jsonToXml(jsonObj) {
    let xml = '';
    for (const prop in jsonObj) {
        if (jsonObj.hasOwnProperty(prop)) {
            const value = jsonObj[prop];
            if (typeof value === 'object' && value !== null) {
                xml += `<${prop}>${jsonToXml(value)}</${prop}>`;
            } else {
                xml += `<${prop}>${value}</${prop}>`;
            }
        }
    }
    return xml;
}
//Here "Transform XML to Json" is basically the name of my pervious action. You can add as per your previuos action name from where you are retrieving the json data.
const jsonData = workflowContext.actions.Transform_XML_To_JSON.outputs.body;
const xmlResult = jsonToXml(jsonData);
return { result: xmlResult };

Finally, in the Compose action, I will be storing the output generated from the JavaScript code and saving your design.

Input :

Navigate to the overview of your workflow and select ‘Run With Payload.’ Set the content type to ‘application/xml,’ enter your XML data in the body, and click ‘Run’ to execute.

Output :

In the input for the Liquid operation, we observe the following XML data:

Following the transformation, the resulting data is provided in JSON format:

Let’s examine the output of the final ‘Compose’ action, where we observe that the content is displayed in XML format.

I’m thrilled to share that I’ve successfully completed the XML to XML transformation using a Liquid Map Template! 🎉😀 While I achieved this with JavaScript code, there are numerous other methods to accomplish the same result.

Thank you so much for stopping by and helping me reach 100 followers! 😄🌟 Your support and engagement mean the world to me😀 . I’m absolutely delighted to share my tech knowledge and discoveries with you all. Keep following and supporting me — more exciting content is on the way! 🚀💡 #StayTuned