Template and its uses

Users can learn useful validations and treatments which can be done with the Freemarker language when you use Template Transformer on the Digibee iPaaS.

Know some validations and treatments that can be done with the Freemarker language when you use Template Transformer.

For the examples you'll see next, consider this input JSON:

{
  "request": {
    "code": 213,
    "value": 4513423.1322,
    "description": "   test request   ",
    "items": [
      {
        "name": "item 1",
        "quantity": 2
      },
      {
        "name": "item 2",
        "quantity": 1
      }
    ]
  }
}

LIST

Enables you to make iterations in an array (list) in JSON. Imagine this function in the creation of a dynamic elements list in the output, that transforms JSON into XML.

Syntax

Example

Using the input informed in the beginning of this article, you can create a dynamic elements list using the template.

What you must inform in the template is:

XML output

Remember that the Template output is an XML in a string.

IF/ELSE

You can use this function to validate some information. Even when its fields are null or empty, there's no great impact on your data transformation.

Syntax

There's no limitation for the quantity of elseif, and it's also possible to use just an if without the else - it all depends on your need.

Example

Using the input informed at the beginning of this article, you can create a validation in which the "code" field needs to be greater than 0 (zero).

What you must inform in the template is:

XML output If the condition is true:

If the conditions is false:

Did you know the use of the function isn't limited to simple conditions? You can also use expressions to validate if the field exists (??) or if it has content (has_content).

Syntax

It's possible to use logical operators for a more advanced condition.

&& - for AND.

|| - for OR.

Example

Now see how to improve the validation so that the if applied in the first case is used only if the "code" field exists and has content.

What you must inform in the template is:

XML output If the condition is true:

If the condition is false...

... and the "code" field has a value that is null, empty, or doesn't come in the input:

TRIM

This function is used to remove the blank spaces at the beginning and in the end of a string.

Syntax

Example

Using the input informed at the beginning of this article, you can remove the blank spaces at the beginning and in the end of the "description" field".

What you must inform in the template is:

XML output

REPLACE

This function is used to replace a pre-established value in the field.

Syntax

Example

Using the input informed at the beginning of this article, you can replace the "test" value in the description with "prod".

What you must inform in the template is:

XML output

SLICE (SUBSTRING)

This function is used when the field must have a determined size.

Syntax

Example

Using the input informed at the beginning of this article, you can determine for the field to have 10 characters only, even if it has a greater size.

A good practice applicable to this function is to use IF/ELSE to validate the desired size for the field. That way, you avoid errors if the field is smaller than the established value.

What you must inform in the template is:

XML output

LOCALE

This function is used in the template to set the location in a numeric value.

Syntax

To use it, you must provide the tag before the dynamic field. Check how to do it in the examples below.

Example

Using the input informed at the beginning of this article, you can format the value to be in the legacy system standard.

What you must inform in the template is:

XML output

Custom

If the desired format isn't accepted by your system, you can define the necessary number for formatting through the "number_format" tag. It allows you to customize the number formatting.

Syntax

The hashtags (#) define the quantity of decimal places to be used.

Example

Using the input informed at the beginning of this article, you can format the value to be in the legacy system standard.

What you must inform in the template is:

XML output

Was this helpful?