Data mapping with the Template modules
Mapping data is usually done with the Property Mapper or Data Mapper modules, which supports the common mappings: renaming, change hierarchies and add/remove properties. Sometimes, especially when you need to create a complex message structure with lots of static data/hierarchies, it can be difficult to see the end result when using these modules. In these situations the ‘Template’ modules can be a better alternative.
With these modules you write the whole message structure as text, JSON in the case of the Message Template module and any text with the Text Template module. Using template syntax: {msgProperty} you can insert dynamic values from your Flow anywhere in the message structure. The difference between these two modules is that the Message Template module requires JSON syntax and will output a Flow message that can be processed in other modules. The Text Template module will output a text string. If you just want to create e.g. a JSON or XML message and send it to an external system the Text Template module is the best choice. If you need to process the message before it is being delivered the Message Template module is the best choice. Then you can use the JSON or XML modules to convert the message to the corresponding text representations just before the output module.
Let’s take a look at an example template:
{ "Factory": {site.name}, "Line": "Packaging", "Asset": {site.machines[0].name}, "Measurements": [ { "Source": {data[0].name}, "Value": {data[0].value}, "Unit": "degC" }, { "Source": {data[1].name}, "Value": {data[1].value}, "Unit": "psi" } ] }
If we then send the following message to a template module with the above template:
{ "data": [ { "name": "temperature", "value": 25.2 }, { "name": "pressure", "value": 101.9 } ], "site": { "machines": [ {"name": "wrapper"}, {"name": "printer"} ], "name": "factory1" } }
We will get the following result from the template module:
{ "Asset": "wrapper", "Factory": "factory1", "Line": "Packaging", "Measurements": [ { "Source": "temperature", "Unit": "degC", "Value": 25.2 }, { "Source": "pressure", "Unit": "psi", "Value": 101.9 } ] }