Bulking multiple data items into a JSON message
How to create a JSON messages which bulks multiple data items together in a single message
This article will demonstrate how to bulk various data items into a single JSON message which can be used as payload for IT communication Connectors such as MQTT & REST API's.
To get started you will need to add an Instance together with data Items to be bulked and wrapped into the JSON message.
📽️ Video instruction
Go to the bottom of the article to check out the video instructions for how to apply data formatting to an Item with Input template
Defining a custom structure for the JSON message
The first thing to decide is the structure of the JSON payload which might either be of your choice or dependent on the other system (such as a cloud service) who is going to receive the data.
In this example we assume we have an Instance called AB_xyz_PLC with a set Items as follows:
Instance: AB_XYZ | |
---|---|
Item name (or UID) | Value |
Temperature | 21° |
Pressure | 10 kPa |
Humidity | 50 %RH |
Speed | 1200 Hz |
Level | 1500 cm |
Now we know what data to include in the message, let's define how we want our JSON message to be:
{
"%TEMPERATURE.NAME%": "%TEMPERATURE.VALUE%",
"%PRESSURE.NAME%": "%PRESSURE.VALUE%",
"%HUMIDITY.NAME%": "%HUMIDITY.VALUE%",
"%SPEED.NAME%": "%SPEED.VALUE%",
"%LEVEL.NAME%": "%LEVEL.VALUE%",
"time": "%VALUE.TIME%"
}
The above format can be edited and structured in any way that suites your JSON format. Any JSON format is supported including arrays.
You can find a list of all system and items variables at the following articles:
Above would translate into something like when receiving the topic at the MQTT broker:
{
"Temperature": "21",
"Pressure": "10",
"Humidity": "50",
"Speed": "1200",
"Level": "1500",
"time": "2024-04-02T12:00:00"
}
Applying the structure of the JSON message
Next step is to apply the structure of your custom JSON message to the messages being transmitted from the edge and into the system which requires the JSON message (MQTT, REST API, etc).
This can be achieved in two ways:
- Setting a default input template for the items to receive the data.
- Adding it as custom template for a mapping
Setting the input template to the JSON message
To set a default data format for a item the Input template parameter in the mapping can be used. This will be the default format of the data unless other is specified in the mapping.
The Input template needs to be set for the Item which is going to receive the data. In this example an MQTT topic.
To do so, follow the below steps:
Use the Input template of the Item if the data formatting is the same across all mappings
Setting JSON message individually per mapping
Data formatting can also be done individually on each mapping by setting the Custom value in the mapping.
To apply individually data formatting of each mapping follow the below steps:
Use Custom value in the Mapping if a Items has multiple Mappings as receiver where the data needs individually formatted. As Sender Item select the Item which defines when to execute the mapping e.g. transmit data to the MQTT broker
Example video
Check out below video to how a MQTT broker and topic is configured to use the Input template to format the data streams to the topic.
In this example we have some simulated data with the items parameters given in the section “Defining a custom structure for the JSON message”. The MQTT broker is a test broker with a topic under sia_connect/example_data_formatting/%UUID% where %UUID% is the UUID of the SIA Connect unit.