Klevr Product Documentation logo

Transform Liquid Stage

The Transform Liquid stage allows a message to be transformed using the Liquid transformation language.

Stage Overview

The Transform Liquid stage allows a message to be transformed using the Liquid transformation language.

There are a number of different transformation options available, which are controlled via the contentType parameter:

  • json-to-json: Accepts an inbound JSON payload, and uses a Liquid transform to produce an outbound payload which is also in JSON. (Can also be defined as just json.)
  • csv-to-json: Accepts an inbound CSV payload, and uses a Liquid transform to produce an outbound payload which is in JSON format.
  • json-to-text: Accepts an inbound JSON payload, and uses a Liquid transform to produce an outbound payload which is in plain text format.
  • json-to-xml: Accepts an inbound JSON payload, and uses a Liquid transform to produce an outbound payload which is in XML format.
  • xml-to-json: Accepts an inbound XML payload, and uses a Liquid transform to produce an outbound payload which is in JSON format.

The format required for defining the Liquid schema file changed in Integration Hub v5.0.0.

Stage Definition File Location

All Transform Liquid stages are defined by creating an appropriately formatted JSON definition file in the following location:

interfaces
{businessUnitCode}
{domainCode}
{integrationName}
tr
lq
{STAGE_NAME}.json

Stage JSON Definition File Format

The JSON definition file for a Transform Liquid stage is as follows:

{
  "name": "stage-name",
  "displayName": "Brief Stage Title",
  "description": "Brief description of the stage.",
  "enabled": "$env:STATUS_<INTEGRATION_NAME>",
  "source": "source-event",
  "concurrency": 10,
  "contentType": "json",
  "discardEmptyPayloads": false,
  "schemaFilename": "schemas/path/to/schema/schema-name.liquid",
  "hasHeader": false,
  "lineEnding": "windows|unix",
  "useBase64PayloadInput": false,
  "templateVariables": {
    "VARIABLE_1": "$env:VARIABLE_1",
    "VARIABLE_2": "$env:VARIABLE_2"
  },
  "preLogTransformSchemaFileName": "schemas/path/to/schema/schema-name.liquid",
  "preLogTransformSchemaType": "liquid|javascript",
  "preLogTransformFormat": "json|xml|csv"
}
PropertyDescription
nameThe name of the stage.
displayNameOptional (but highly recommended). A short (3 or 4 word) "display name" for the stage, used as a "title" of the stage in the Integration Hub Portal service's self-documentation capability.
descriptionOptional (but highly recommended). A brief (10 to 15 word) description of the stage, used as a brief description of the stage's purpose in the Integration Hub Portal service's self-documentation capability. For a more detailed explanation (when needed), use the stage markdown feature.
enabledOptional boolean flag (true or false). The default is true.

Generally set via a DevOps variable. Controls if the stage is deployed enabled or not.
sourceThe source event that the stage subscribes to. See Pub/Sub Model.

Alternatively, sources may be supplied, and an array of one or more source events can be subscribed to.
concurrencyOptional number of concurrent runs permitted for the stage. Default is 10.
contentTypeDefines the incoming payload format and expected output format of the transformation.

One of json-to-json (or just json), csv-to-json, json-to-text, json-to-xml, or xml-to-json.
discardEmptyPayloads
Only supported when contentType is json-to-json (or just json).
Optional boolean flag (true or false). The default is false. When true, if the transformation results in producing a JSON payload that is an empty string, an empty JSON object (i.e. {}), or an empty JSON array (i.e. []), then the stage will "discard" the payload by not publishing an event to trigger further processing.
schemaFilenameA filename that defines the Liquid transformation schema. See below for more details on the schema file location and format.
hasHeaderOptional boolean flag (true or false). The default is false.

When the contentType value is set to csv-to-json the expectation is that the preceding stage is supplying a CSV-formatted payload. In the event that the CSV formatted payload has a header row, hasHeader must be true to allow the Liquid transformation schema defined in schemaFilename to be able to parse the payload correctly.
lineEndingRequired when the contentType value is csv-to-json. Set to either windows or unix.

Defines if the Liquid transformation set by the schemaFilename field should expect the CSV file being transformed to have Windows (\r\n) or Unix (\n) line endings.
useBase64PayloadInputOptional boolean flag (true or false). Default is false.

Set to true when the preceding stage is supplying a Base64 encoded value, and the Liquid transformation code is expecting and will handle the payload in that Base64 encoded format.

If not set to true, the stage will decode the Base64 value prior to passing it into the Liquid code for processing.
templateVariablesAn optional object of key/value pairs. Prior to deploying the stage, the Liquid transformation schema will be updated to replace any instances of <<KEY_NAME>> in the schema with the corresponding value from this object.

This allows a Liquid transformation schema to be more easily used across different stages where only minor differences in the transformation are required.

Pre-Logging Transformation

This stage supports the optional pre-logging transformation feature using the following fields:

  • preLogTransformSchemaFileName
  • preLogTransformSchemaType
  • preLogTransformFormat

See the Pre-Logging Transformation for details.

Transformation Schema File Location

The schemaFilename must be defined using a directory path which is based on the root of the Integration Hub service folder. It is recommended that this always starts with the directory schemas, and that all transform schemas be stored in a logical structure that allows for easy reuse of transform schema definitions as required.

Transformation Schema File Format

Integration Hub uses DotLiquid for Liquid stages.

Please be aware of the following behaviours when developing Liquid schemas for DotLiquid:

  • The incoming payload into the Liquid schema is in the input variable. (This has changed from Integration Hub v4.0.0, where the content variable was used).
  • The incoming and outgoing payloads are always in JSON format at the point that they are presented to the Liquid transformation, and at the point they complete being transformed by the Liquid transform.
    • Even if you are performing an xml-to-json transformation, the incoming XML payload is automatically transformed into a JSON representation of the XML before being passed through in the input variable, so, your Liquid templates will need to parse/use the incoming payload accordingly.
    • Similarly, even if you are performing a json-to-xml transformation, you must return a JSON-based payload, being a JSON representation of the intended eventual XML output. The system will then transform the JSON output into XML for you.
  • In addition to the input variable which contains the incoming payload, the cloudEvent variable is also available, and contains metadata relating to the overall integration's execution to date. This includes file metadata from file-based inbound stages, and may be useful when an integration run's original source filename is required later in the integration process.
  • DotLiquid implements strict existence checking when accessing values. As a result, the Liquid template needs to perform a test for existence before attempting to access or loop over a variable, if it is possible that the variable may not exist. This can be done in the following way:
{% if employee.PreferredLastName %}
    {% assign preferredLastName = employee.PreferredLastName %}
{% endif %}

Tips and Tricks

Checking for Missing Elements

DotLiquid will error if you attempt to use an element, including checking the value of the element, if the element does not exist in the payload.

If a payload field may not always exist in the input, you can check to confirm if the field exists safely, before attempting to access the field, using the following approach:

{% if input.preferredLastName %}
    "preferredLastName": "{{ input.preferredLastName }}",
{% endif %}

Date Formatting

DotLiquid uses standard .NET date/time format strings with the Date filter. So, use (for example):

"EmploymentStartDate": "{{ input.EmploymentStartDate | Date: 'yyyy-MM-ddTHH:mm:ssZ' }}"

Stripping Non-ASCII Text

Klevr Integration Hub's implementation of DotLiquid has been extended with a custom ASCII filter, which can be used to remove all non-ASCII characters from a string. For example:

"Output": "{{ String With Non-ASCII Characters | ASCII }}"

XML Headers

As noted above, when performing a json-to-xml transformation, you must return a JSON-based payload, being a JSON representation of the intended eventual XML output. The system will then transform the JSON output into XML for you.

If you want to include an XML header in the resultant XML, then generate a JSON-based payload like this:

{
  "?xml": {
    "@version": "1.2",
    "@encoding": "UTF-16"
  },
  "Root_Item": {
    "Level_1_Item": "VALUE"
  }
}

This will produce output XML of:

<?xml version="1.2" encoding="UTF-16"?>
<Root_Item>
  <Level_1_Item>VALUE</Level_1_Item>
</Root_Item>

Last updated on