Liquid Templates In Logic Apps

Introduction

Azure Logic Apps support basic JSON transformations through native data operation actions such as Compose or Parse JSON. For advanced JSON transformations, we can use Liquid templates with our logic apps. Microsoft has released the support of Liquid Templates to transform JSON and XML objects.

Liquid is a templating engine that uses a simple markup language to produce all sorts of output formats, such as HTML, JSON, CSV, etc. More details can be found here. It’s quite easy to use, we just need to get used to the new syntax.

Azure

Within Logic Apps, Liquid templates are used as a kind of XSLT for JSON objects. This allows us to create more complex transformations on JSON objects, without the need of introducing a custom Azure Function. Liquid templates are part of the Integration Account. Behind the scenes, DotLiquid is used.

How to Use

Here we will see how to use a Liquid map or template, which supports more complex JSON transformations, such as iterations, control flows, variables, and so on.

Prerequisites

Before we perform a Liquid transformation in the logic app, we must define the mapping from JSON to JSON with a Liquid map and store that map in integration account. And for that we must have the below prerequisites;

  • An Azure subscription.
  • Basic knowledge about logic apps
  • A basic Integration Account
Define Liquid template

Create the Liquid template like below. The Liquid template defines how to transform JSON input as described here,

  1. {     
  2.    "Employee Name" : "{{ content.FirstName }} {{ content.LastName }}",  
  3.    "Company Name" : "{{ content.Company }}",  
  4.    "Date Of Joining" : "{{ "now" | Date: "MM/dd/yyyy" }}",  
  5.    "Department" : "{{ content.department }}",  
  6.    "Technology" : "{{ content.Work | Size }}",  
  7.    "Skills" : [     
  8.       {% for Skill in content.Work %}        
  9.       {            
  10.          "Name" : "{{ Skill.skil }}",   
  11.          "Marks" : {{ Skill.Mark }}        
  12.       },     
  13.       {% endfor %}     
  14.    ]  
  15. }  

Save the above liquid template as “JsonToJsonTemplate.liquid”.

Use the below sample JSON file as an input for this demo.

  1. {   
  2.    "FirstName" : "Kamlesh",   
  3.    "LastName" : "Kumar",   
  4.    "Company" : "Microsoft",  
  5.    "department" : "Interface Technology",  
  6.    "Work" : [   
  7.       {   
  8.          "skil" : "BizTalk Server",   
  9.          "Mark" : 95  
  10.       },   
  11.       {   
  12.          "skil" : "Logic Apps",   
  13.          "Mark" : 95  
  14.       },  
  15.       {   
  16.          "skil" : "Azure",   
  17.          "Mark" : 85  
  18.       },  
  19.       {   
  20.          "skil" : "OMS",   
  21.          "Mark" : 80  
  22.       },  
  23.       {   
  24.          "skil" : "Share Points",   
  25.          "Mark" : 70  
  26.       },  
  27.       {   
  28.          "skil" : "C Sharp",   
  29.          "Mark" : 95  
  30.       },  
  31.       {   
  32.          "skil" : "SQL Server",   
  33.          "Mark" : 95  
  34.       }  
  35.    ]  
  36. }  

Create / Add Map to Integration Account

Open Azure portal and sign in with your existing account, if you don't have an account on an Azure portal, first, sign up and then, register on Azure portal.

Click on "All resources" in the left panel menu like the below screenshot.

Azure

You should have your integration account created. In the search box, find and select your integration account like in the above screenshot.

Once you have clicked the integration account, you could see some components in the right panel and here you have to click on Map.

Azure

In the new panel, you can find your existing map details or if you are here for the first time, then you can add map. Click on Add and provide details for your map like the below screenshot.

Azure

  • Name
    The name for your map, which is "SimpleJsonToJsonTemplate_New" in this example.

  • Map type
    The type for your map. For JSON to JSON transformation, you must select liquid.

  • Map
    An existing Liquid template or map file to use for transformation, which is "JsonToJsonTemplate.liquid" in this example. To find this file, you can use the file picker.
Create Logic Apps with JSON transformation

Now, here, we have to create a logic app which will be used in JSON transformation. Create a logic app. If you don't know how, please read my previous article "Azure Logic Apps - Hello World".

Add the Request trigger to your logic app.

Azure

Azure

Next, add an action.

Azure

In the search box, type liquid, and select ‘Liquid - Transform JSON to JSON’.

Azure

In the Content box, select Body from dynamic content list or parameters list, whichever appears. From the Map list, select your Liquid template, which is "SimpleJsonToJsonTemplate_New" in this example.

Azure

If the list is empty, your logic app is likely not linked to your integration account. To link your logic app to the integration account that has the Liquid template or map, follow these steps:

Go to your logic app menu, select Workflow settings from Settings section.

Choose your Integration account name from the Select an Integration account list, and click on Save. If still you are not able to see your integration account on the list, then please make sure your logic app and Integration account are at the same location.

Azure

Test your logic app

It’s time to test your logic app liquid JSON to JSON transformation. Use Postman or a similar tool to post JSON input to your logic app. In this example I'm using the Postman tool.

Azure

The transformed JSON output from your logic app looks like this example:

 Azure

Azure

Summary

In this article, we saw how to create a user profile in Logic Apps using Liquid templates. I hope you’ve found this post handy. Feel free to add your comments or ask any questions below. If there's anything specifically that you want me to cover with respect to Liquid transform or Logic Apps please let me know.