Data formats
MQTT allows the transport of binary data, whereby the actual structure of the message content is not fixed by the specification. Thus, any data can be transported, e.g. cyclic or event-based telemetry data or commands to the controller. Since the data format is not specified, the format must be known to the sender and receiver.
Over the years, a multitude of data formats and corresponding data description languages have emerged. In many IoT applications, JSON and XML have established themselves as description languages. However, the structure of a corresponding JSON or XML document is still application-specific and must be known to the applications so that they can exchange data with each other.
JSON
JSON (JavaScript Objection Notation) is a lightweight description language in an easy-to-read text form in which data is organized into so-called objects via property/value pairs. JSON has established itself in most IoT applications due to its easy readability and still low overhead (compared to other description languages such as XML). The following sample shows a JSON document containing telemetry values from three sensors including metadata and timestamps.
{
"Timestamp": "2017-04-04T12:42:42",
"Values": {
"Sensor1": 42.41999816894531,
"Sensor2": 230,
"Sensor3": 3
},
"MetaData": {
"Sensor1": {
"Unit": "m/s",
"DisplayName": "Speed"
},
"Sensor2": {
"Unit": "V",
"DisplayName": "Voltage"
},
"Sensor3": {
"Unit": "A",
"DisplayName": "Current"
}
}
}
XML
XML (Extensible Markup Language) is a description language for structuring data in order to make it readable for both humans and programs. XML has a much higher overhead than JSON and is hardly used in modern IoT applications. The sample shown above could be represented in XML as follows.
<XmlDocument>
<Timestamp>2017-04-04T12:42:42</Timestamp>
<Values>
<Value Name="Sensor1">42.41999816894531</Value>
<Value Name="Sensor2">230</Value>
<Value Name="Sensor3">3</Value>
</Values>
<MetaDataColl>
<MetaData Name="Sensor1">
<Unit>m/s</Unit>
<DisplayName>Speed</DisplayName>
</MetaData>
<MetaData Name="Sensor2">
<Unit>V</Unit>
<DisplayName>Voltage</DisplayName>
</MetaData>
<MetaData Name="Sensor3">
<Unit>A</Unit>
<DisplayName>Current</DisplayName>
</MetaData>
</MetaDataColl>
</XmlDocument>
Of course, there are many other variants of how the above JSON can be converted into an equivalent XML (and here it also becomes clear why nevertheless sender and receiver must know the data format - despite JSON or XML). Only the basic principle is to be shown here.
If you compare the two documents above, you will quickly notice the difference in size: while the JSON document has an approximate size (depending on any line breaks and spaces) of about 393 bytes, the equivalent XML document has a size of about 569 bytes and is thus about 44% larger (although the same information is transferred).
![]() | PLC library Tc3_JsonXml The library Tc3_JsonXml, which is automatically installed with TwinCAT 3 XAE, facilitates creation and processing of JSON and XML objects. (See documentation PLC Lib: Tc3_JsonXml) |