Using Liquid filters in API Management to transform messages whitepaper

Posted: February 3, 2023  |  Categories: API Management Azure

Those who know me and follow me are used to seeing me publishing several resources, including whitepapers. But this one is different! Typically, when it comes to whitepapers or books, I usually release them in BizTalk360 or Serveless360, a partnership that I have since many years, and you should expect to find it more in the future. However, this is the first whitepaper I have published under the DevScope umbrella, and I could be so proud of the result.

Note: not only I’m proud of the end result of this whitepaper, which I personally find this information very useful, but I’m also in love with my new corporate photo!

I typically write about “stuff” I work on daily bases, and for the past months, I have been working a lot with Liquid Template to perform transformations on API Management. Liquid is an open-source template language created by Shopify and written in Ruby. It is the backbone of Shopify themes and is used to load dynamic content on storefronts. However, Azure API Management uses a subset of Liquid templating language to transform the body of a request or response: DotLiquid

If it is true that we can find excellent Shopify documentation, the same may not be that simple for DotLiquid, and I struggle a little to get decent examples of how to use Liquid Filters in API Management, and that was the main reason why I decided to write this whitepaper.

What is DotLiquid?

DotLiquid is a deviation from Shopify’s original liquid template language. It is a .Net port of the popular Shopify’s Liquid templating language. It is a separate project that aims to retain the same template syntax as the original while using .NET coding conventions where possible. However, they are not entirely the same. In some cases, they have different behaviors.

Liquid uses a combination of objects, tags, and filters inside template files to display dynamic content.

Where:

  • Objects contain the content that Liquid displays on a page. Objects and variables are displayed when enclosed in double curly braces: {{ and }}.
Hi my name is {{ author.name }}
  • Tags create the logic and control flow for templates. The curly brace percentage delimiters {% and %} and the text that they surround do not produce any visible output when the template is rendered. This lets you assign variables and create conditions or loops without showing any of the Liquid logic on the page.
{% if author %}
 Hi my name is {{ author.name }}!
{% endif %}
  • Filters change the output of a Liquid object or variable. They are used within double curly braces {{ }} and variable assignment and are separated by a pipe character |.
{{ "Sandro" | Append: " Pereira" }}

In this whitepaper, I’m going to focus on the DotLiquid Filters you can use on API Management to transform the body of a request or response.

What is a liquid filter?

As mentioned above, Filters change the output of a Liquid object or variable. They are used within double curly braces {{ }} and variable assignment and are separated by a pipe character |. They are like functions in C# code or Functoids in BizTalk Server maps.

If we apply, for example, the Append filter like:                                                              

{% assign myvariable = "Sandro" %}
{{ myvariable | Append: " Pereira" }}   

The end result will be: Sandro Pereira

We can also apply multiple filters on one output which are applied from left to right. Like a PowerShell pipeline. A PowerShell pipeline is a series of commands connected by pipeline operators (|). Each pipeline operator sends the results of the preceding command to the next command. Filters use the same concept. For example:

"Name": "{{ "Sandro Pereira" | Split: " " | Last }}"

The end result here will be: “Name”: “Pereira”

Download

You can see the official release announcement here: Download Sandro Pereira’s whitepaper on Liquid Filters.

You can download, for free, the Using Liquid filters in APIManagement to transform messages whitepaper here:

Hope you find this useful! So, if you liked the content or found it useful and want to help me write more content, you can buy (or help buy) my son a Star Wars Lego! 🙂

Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top