Output Templates
Some outputs support a special Template syntax to format the payload. This can be useful when the output format needs to be finally controlled and it can’t be accomplished in pipelines. For example, if the endpoint expects a special SOAP or XML format.
The ‘Template’ setting for these Outputs uses Apache FreeMarker to provide this control.
Apache FreeMarker Special Values
-
${value}
- The value being written out. By itself this evaluates to the default JSON output
-
${values}
- Used in cases where the output is an array of values versus a single object. Like
${value}
this by default evaluates to the default JSON output
- Used in cases where the output is an array of values versus a single object. Like
-
${value.attribute_name}
- Index into a model to get the value for
attribute_name
. For example, a model with a cycleCount attribute would be referenced as${value.cycleCount}
- Index into a model to get the value for
-
${value.name}
- Returns the name of the model instance or alias
-
${value.quality}
- Returns the quality as a string, either Good or Bad
-
${value.time}
- Returns the timestamp as an epoch (UTC)
-
${value.type}
- Returns the model name
-
${value.elements}
- Returns a list of key value pairs, where key is the attribute name and value is the attribute value
-
${value.array}
- Returns an array required by FreeMarker array operations
-
${value.rawString}
- For use with file typed values to access the raw string data contained in the file
Note: The syntax can be chained together. So if the model has a child model called “motor” with an attribute “amps”, you can access amps using ${value.motor.amps}
.
Examples
With this API, here are some example use cases. Please see the FreeMarker documentation online for FreeMarker specific syntax.
Decorate payload (add around payload)
{
"index": "myindex",
"source": "mysource",
"payload": [
<#list values as value>
${value}<#if value_has_next>,</#if>
</#list>
]
}
Rename a field
{
"payload": [
<#list values as value>
${value?replace("_name", "name")?replace("_model","model")?replace("_timestamp", "timestamp")}
<#ifvalue_has_next>,</#if>
</#list>
]
}
Xml
<root>
<values>
<#list values as value>
<value>
<#list value.elements as name, v>
<#if name != "motor">
<${name}>${v.string}</${name}>
</#if>
</#list>
<motor>
<#list value.motor.elements as name, v>
<${name}>${v.string}</${name}>
</#list>
</motor>
</value>
</#list>
</values>
</root>
Values in OPC format
{
"timestamp": ${.now?long},
"values": [
<#list values as value>
{
"id": "${value.name}",
"v": ${value},
"q": ${value.quality},
"t": ${value.time}
}<#if value_has_next>,</#if>
</#list>
]
}
Values in influxdb format
${value.type},name=${value.name} <#list value.elements as name,v>${name}=${v}<#if name_has_next>,</#if></#list> ${value.time}
Transform time
{
"values": [
<#list values as value>
{
"id": "${value.name}",
"v": ${value},
"q": ${value.quality},
"t": "${value.time?number_to_datetime?iso_utc}"
}<#if value_has_next>,</#if>
</#list>
]
}