Use Liquid templates as maps in workflows to transform JSON to JSON/TEXT and XML to JSON/TEXT in Azure Logic Apps

Azure Logic Apps is a cloud-based service that allows you to automate workflows and integrate apps, data, services, and systems. Liquid is an open-source templating language that can be used within Logic Apps to transform JSON and XML data. Below is a guide on how to use Liquid templates to transform JSON to JSON, JSON to TEXT, XML to JSON, and XML to TEXT in Azure Logic Apps.

1. JSON to JSON Transformation

Input JSON

{
  "name": "John Doe",
  "age": 30,
  "address": {
    "city": "New York",
    "country": "USA"
  }
}

Liquid Template (json-to-json.liquid)

{
  "fullName": "{{ content.name }}",
  "yearsOld": {{ content.age }},
  "location": {
    "city": "{{ content.address.city }}",
    "country": "{{ content.address.country }}"
  }
}

Output JSON

{
  "fullName": "John Doe",
  "yearsOld": 30,
  "location": {
    "city": "New York",
    "country": "USA"
  }
}

Steps in Azure Logic Apps

  1. Add a Transform JSON to JSON action.
  2. Upload the json-to-json.liquid template in Integration Account under map section.
  3. Map the input JSON to the template.
  4. Run the workflow to see the transformed JSON.

2. JSON to TEXT Transformation

Input JSON

{
  "name": "Jane Smith",
  "age": 25,
  "address": {
    "city": "London",
    "country": "UK"
  }
}

Liquid Template (json-to-text.liquid)

Name: {{ content.name }}
Age: {{ content.age }}
City: {{ content.address.city }}
Country: {{ content.address.country }}

Output TEXT

Name: Jane Smith
Age: 25
City: London
Country: UK

Steps in Azure Logic Apps

  1. Add a Transform JSON to TEXT action.
  2. Upload the json-to-text.liquid template in Integration Account under map section.
  3. Map the input JSON to the template.
  4. Run the workflow to see the transformed TEXT.

3. XML to JSON Transformation

Input XML

<root>
  <name>John Doe</name>
  <age>30</age>
  <address>
    <city>New York</city>
    <country>USA</country>
  </address>
</root>

Liquid Template (xml-to-json.liquid)

{
  "fullName": "{{ content.root.name }}",
  "yearsOld": {{ content.root.age }},
  "location": {
    "city": "{{ content.root.address.city }}",
    "country": "{{ content.root.address.country }}"
  }
}

Output JSON

{
  "fullName": "John Doe",
  "yearsOld": 30,
  "location": {
    "city": "New York",
    "country": "USA"
  }
}

Steps in Azure Logic Apps

  1. Add a Transform XML to JSON action.
  2. Upload the xml-to-json.liquid template in Integration Account under map section.
  3. Map the input XML to the template.
  4. Run the workflow to see the transformed JSON.

4. XML to TEXT Transformation

Input XML

<root>
  <name>Jane Smith</name>
  <age>25</age>
  <address>
    <city>London</city>
    <country>UK</country>
  </address>
</root>

Liquid Template (xml-to-text.liquid)

Name: {{ content.root.name }}
Age: {{ content.root.age }}
City: {{ content.root.address.city }}
Country: {{ content.root.address.country }}

Output TEXT

Name: Jane Smith
Age: 25
City: London
Country: UK

Steps in Azure Logic Apps

  1. Add a Transform XML to TEXT action.
  2. Upload the xml-to-text.liquid template in Integration Account under map section.
  3. Map the input XML to the template.
  4. Run the workflow to see the transformed TEXT.

How to Set Up in Azure Logic Apps

  1. Create a Logic App:
    • Go to the Azure portal and create a new Logic App.
  2. Add a Trigger:
    • Use an HTTP request trigger or any other trigger to start the workflow.
  3. Add Transformation Actions:
    • For JSON transformations: Use the Transform JSON to JSON or Transform JSON to TEXT action.
    • For XML transformations: Use the Transform XML to JSON or Transform XML to TEXT action.
  4. Upload Liquid Templates:
    • Go to the Integration Account in Azure and upload your .liquid files.
    • Link the templates to the transformation actions in your Logic App.
  5. Map Input Data:
    • Provide the input JSON or XML data to the transformation action.
  6. Test and Run:
    • Run the workflow and verify the output.

Sample Workflow

  1. Trigger: HTTP request with JSON or XML payload.
  2. Action: Transform JSON/XML to JSON/TEXT using a Liquid template.
  3. Output: Send the transformed data to another system or store it.

Here's how Logic Apps should look:

estudy247-la-test-liquid-22
estudy247-la-test-liquid-23
You can download the logic app template from the estudy247 GitHub repository – la-test-liquid-22 and la-test-liquid-23

Key Notes

  • Use Visual Studio Code with the Liquid extension to create and test Liquid templates.
  • Ensure the input data structure matches the template’s expected structure.
  • Test the Logic App workflow with sample data before deploying it to production.
Post a comment

Leave a Comment

Scroll to Top