Message Transformation from JSON to JSON using FreeMarker in WSO2 MI.

Message Transformation from  JSON to JSON using FreeMarker in WSO2 MI.

FreeMarker is a Java-based template engine that allows you to generate text-based output (such as HTML, XML, JSON, etc.) dynamically. The integration of FreeMarker templates within WSO2 MI was introduced in version 4.0.0. WSO2 MI is an integration runtime that allows users to develop, deploy, and manage integrations in microservices architectures. Developers can generate dynamic content based on predefined templates within their integration flows.

Walkthrough:

JSON to JSON

Let’s do simple transformation considering below input and output

                          INPUT                          OUTPUT
{     “Addresses”: [         {             “Title”: “Present Address”,             “street”: “Sainagar”,             “district”: “Hyderabad”,             “state”: “Telangana”,             “pincode”: “505001”         },         {             “Title”: “permeant Address”,             “street”: “Sai”,             “district”: “Karimnagar”,             “state”: “Telangana”,             “pincode”: “505001”         }     ],     “success”: true,     “result”: “SUCCESS” }  {     “TotalResults”: “2”,     “Addresses”: [         {             “Title”: “Present Address”,             “Address”: “Sainagar Hyderabad”,             “State”: “Telangana-505001”         },         {             “Title”: “permeant Address”,             “Address”: “Sai Karimnagar”,             “State”: “Telangana-505001”         }     ],     “success”: true,     “result”: “SUCCESS” }  

The below Freemarker template used for the transformation

{

“TotalResults”: <#if payload.Addresses?has_content>”${payload.Addresses?size}”<#else>null</#if>,

“Addresses”: [

<#list payload.Addresses as d>

     {

       “Title”:  <#if d.Title?has_content>”${d.Title}”<#else>””</#if>,

       “Address”: <#if d.street?has_content || d.district?has_content>”${d.street!}   ${d.district!}”<#else>””</#if>,

        “State”: <#if d.state?has_content || d.picode?has_content>”${d.state! }-${d.pincode}”<#else>””</#if>

              }<#sep> ,</#sep>

</#list>

],

“success”: <#if payload.success?has_content>${payload.success?c}<#else>””</#if>,

“result”: <#if payload.result?has_content>”${payload.result}”<#else>””</#if>

}

In the above FreeMarker template used functions as follows:

  • Conditional check (<#if><#else></#if>)
  • Iteration over lists(<#list>)
  • Variable interpolation(${})
  • <#sep> ,</#sep>
  • OR(||) operator

Conditional check

<#if d.Title?has_content>”${d.Title}”<#else>””</#if>

This expression checks if the variable d.Title has content. If it does, It inserts the value of d.Title into the output string otherwise, it inserts an empty string.

Iteration over lists

<#list Payload.Addresses as d>

    <!–logic–>

</#list>

This line starts the loop over the list of addresses. Each address is referenced as `d` inside the loop.

Variable interpolation

${} allows you to insert the value of a variable into the template output. For example, ${ Payload.result} would insert the value of the result property of the Payload object into the template output.

<#sep> ,</#sep>

Specifies that a comma should be inserted after each address object, except for the last one.

OR(||) operator

<#if d.street?has_content || d.district?has_content>”${d.street!}   ${d.district!}”<#else>””</#if>

this expression checks if either d.street or d.district has content. If either of them has content, it inserts the values of d.street and d.district into the output string separated by a space. If both are empty, it provides an empty string as the result.

Create new project Right click on api à new à RestAPI à Create new arifact given required filelds as Name and context à Finish. 

Drag and drop the payload factory

Configure the properties:

  • Payload Format: Inline
  • MediaType: Json
  • Template Type: Freemarker
  • Place the above freemarker transformation code in payload.

Build and run the artifacts

  • Right-click FreemarkerTemplateCompositeExporter and click Export Project Artifacts and Run
  • You will see the following dialog box. Select the FreemarkerTemplateConfigs folder in the artifact list and click Finish.

Result:

For more information, please write to [email protected].