Tc3JsonXmlSampleJsonDataType
Sample of the automatic conversion of structures into a JSON object
This sample illustrates how a data structure can be converted into a JSON object (and vice versa). In the conversion, the layout of a structure is converted into an identical JSON equivalent. Additional metadata can be created via PLC attributes on the member variables of the structure. Vice-versa, in the second part of the example, an existing data structure in the PLC is filled with values from a suitable JSON object.
Layout of the data structure
TYPE ST_Values :
STRUCT
{attribute 'Unit' := 'm/s'}
{attribute 'DisplayName' := 'Speed'}
Sensor1 : REAL;
{attribute 'Unit' := 'V'}
{attribute 'DisplayName' := 'Voltage'}
Sensor2 : DINT;
{attribute 'Unit' := 'A'}
{attribute 'DisplayName' := 'Current'}
Sensor3 : DINT;
END_STRUCT
END_TYPEGenerating the JSON document from a data structure
First, an instance of the ST_Values data structure is converted directly into a JSON object, then the associated metadata is added to the JSON document. Then, two ways of generating the JSON document are shown, starting with the fbJson instance of the function block FB_JsonSaxWriter. The GetDocument() method can be used with a JSON document with no more than 255 characters. However, the CopyDocument() method must be used for larger JSON documents.
IF bStart
// Part 1: create JSON with meta data from structure
bStart := FALSE;
stValues.Sensor1 := 42.42;
stValues.Sensor2 := 50;
stValues.Sensor3 := 7;
fbJson.ResetDocument();
fbJson.StartObject();
fbjson.AddKeyDateTime('Timestamp', dtTimestamp);
// Adding stValues to JSON document
fbJsonDataType.AddJsonKeyValueFromSymbol(fbJson, 'Values', 'ST_Values', SIZEOF(stValues), ADR(stValues));
// Adding metadata from ST_Values datatype to JSON document
fbJsonDataType.AddJsonKeyPropertiesFromSymbol(fbJson, 'MetaData', 'ST_Values', 'Unit|DisplayName');
fbJson.EndObject();
sJsonDoc := fbJson.GetDocument();
fbJson.CopyDocument(sJsonDoc2, SIZEOF(sJsonDoc2));
END_IFResulting JSON document
{
"Timestamp": "2017-04-04T12:42:42",
"Values": {
"Sensor1": 42.42,
"Sensor2": 50,
"Sensor3": 7
},
"MetaData": {
"Sensor1": {
"Unit": "m/s",
"DisplayName": "Speed"
},
"Sensor2": {
"Unit": "V",
"DisplayName": "Voltage"
},
"Sensor3": {
"Unit": "A",
"DisplayName": "Current"
}
}
}Writing JSON document values to a data structure
As an alternative, the method AddJsonValueFromSymbol() can also be used to generate a JSON object directly from a data structure. This resulting JSON object is used to fill another instance of the same data structure with values in the next step.
fbJson.ResetDocument();
// Adding stValues to JSON document
fbJsonDataType.AddJsonValueFromSymbol(fbJson, 'ST_Values', SIZEOF(stValues), ADR(stValues));
sJsonDoc3 := fbJson.GetDocument();
// Using JSON to set values in stValuesRcv
fbJsonDataType.SetSymbolFromJson(sJsonDoc3, 'ST_Values', SIZEOF(stValuesRcv), ADR(stValuesRcv));The resulting JSON object looks like this:
{
"Sensor1": 42.42,
"Sensor2": 50,
"Sensor3": 7
}The string variable sJsonDoc3 contains the JSON object, which is transferred into the structure instance stValuesReceive by calling the method.
![]() | Target data structure The target data structure must match the structure of the JSON document exactly. Otherwise SetSymbolFromJson() returns FALSE. |
