Close

Liquid Templates

Liquid templates are typically used to transform xml to json or json to json. You don’t need schema files to create a Liquid template, just instance messages. Unfortunately there’s no free tooling available to create Liquid templates. You can for instance use Notepad++ and format the Liquid template via the JSTool plugin. Save your Liquid map with the .liquid extension.

Example input file json:
{
“FirstName” : “Kamlesh”,
“LastName” : “Kumar”,
“Company” : “Microsoft”,
“department” : “Interface Technology”,
“Work” : [
{
“skil” : “BizTalk Server”,
“Mark” : 95
},
{
“skil” : “Logic Apps”,
“Mark” : 95
}
] }

Example input xml file:
<?xml version=”1.0″ encoding=”UTF-8″?>
<root>
<FirstName>Kamlesh</FirstName>
<LastName>Kumar</LastName>
<Company>Microsoft</Company>
<department>Interface Technology</department>
<Work>
<element>
<Mark>95</Mark>
<skil>BizTalk Server</skil>
</element>
<element>
<Mark>95</Mark>
<skil>Logic Apps</skil>
</element>
</Work>
</root>

The liquid template is almost identical, except that you refer to the input message by using content.root for xml and content for json.

Example JsonToJson.liquid
{
“Employee Name” : “{{ content.FirstName }} {{ content.LastName }}”,
“Company Name” : “{{ content.Company }}”,
“Date Of Joining” : “{{ “now” | Date: “MM/dd/yyyy” }}”,
“Department” : “{{ content.department }}”,
“Technology” : “{{ content.Work | Size }}”,
“Skills” : [
{% for Skill in content.Work %}
{
“Name” : “{{ Skill.skil }}”,
“Marks” : {{ Skill.Mark }}
},
{% endfor %}
] }

Example XmlToJson.liquid
{
“Employee Name” : “{{ content.root.FirstName }} {{ content.root.LastName }}”,
“Company Name” : “{{ content.root.Company }}”,
“Date Of Joining” : “{{ “now” | Date: “MM/dd/yyyy” }}”,
“Department” : “{{ content.root.department }}”,
“Technology” : “{{ content.root.Work | Size }}”,
“Skills” : [
{% for Skill in content.root.Work %}
{
“Name” : “{{ Skill.skil }}”,
“Marks” : {{ Skill.Mark }}
},
{% endfor %}
] }

In Liquid you can use tags and filters. Tags create the logic and control flow for templates. They are denoted by curly braces and percent signs: {% and %}.

Examples:
{% for orderLine in content.rows %} … {% endfor %}

“State”:
{% if content.confirmed == true %}
“Confirmed”
{% else %}
“Open”
{% endif %}

Filters change the output of a Liquid object. They are used within an output and are separated by a |.

Examples:
“QuantityOrdered”: “{{orderLine.amount | Times: orderLine.packCount}}”

In the documentation filters are always show in lower case, but that doesn’t work. Always use upper case for filters.

Further reading:
Shopify Liquid

Leave a Reply

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