azurexsltmapsazure-logic-appsliquid-template

Liquid template to get XML node and child nodes xml as it is to json format


I am trying Liquid template to convert XML to Json with some transformation. I have sample XML as shown below

Sample XML:

<Employees>
 <Employee>
  <name>abc</name>
  <summary>
   <Age>15</Age>
   <tag1>dd</tag1>
   <tag2>dd</tag2>
   <tag2>dd</tag2>
  </summary>
 </Employee>
</Employees>

My Liquid template

{
"Root": 
    [
     {% for employees in content.Employees %}
    {
        "Name": "{{employees.name}}",
        "Summary": "summary is {{employees.summary}}",
  "Age": "{{employees.summary.Age}}"
    },
    {% endfor %}
    ]
}

I got the Output as below

{
"Root": [
 {
  "Name": "abc",
  "Summary": "summary is ",
  "Age": "15"
 }
 ]
}

For Summary json node I want to display complete summary xml node and child nodes as it is(xml format), but now I am receiving empty. I tried searching to achieve this in liquid template and didn't get any option to achieve this. If not possible then what alternatives can be used in Azure logic apps.

Expected output:

 {
"Root": [
 {
  "Name": "abc",
  "Summary": "summary is <summary><Age>15</Age><tag1>dd</tag1><tag2>dd</tag2><tag2>dd</tag2></summary>",
  "Age": "15"
 }
 ]
}

Solution

  • XSLT 3 can transform your XML to JSON with

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="3.0">
        
      <xsl:strip-space elements="*"/>
    
      <xsl:output method="json" indent="yes"/>
    
      <xsl:template match="/Employees">
        <xsl:sequence
          select="map { 'Root' : 
                      array { 
                          Employee ! map { 'Name' : data(name), 'Summary' : 'Summary is ' || serialize(summary), 'Age' : number(summary/Age) }
                      }
                  }"/>
      </xsl:template>
      
    </xsl:stylesheet>
    

    Result:

     {
      "Root": [
         {
          "Summary":"Summary is <summary><Age>15<\/Age><tag1>dd<\/tag1><tag2>dd<\/tag2><tag2>dd<\/tag2><\/summary>",
          "Name":"abc",
          "Age":15
         }
       ]
     }
    

    https://xsltfiddle.liberty-development.net/6q1SDkM/1

    I think you can use XSLT 3 by now, based on Saxon 9 .NET, in Azure apps.