<< Click to Display Table of Contents >>
JSON Template |
JSON (JavaScript Object Notation) is a format for data exchange that is easily understood by humans, and also simple so that programs can perform information processing and extraction. The JSON format is build on top of two structures, a collection of pairs containing name and value and an ordered list of these pairs.
An Object is an unordered list of pairs of names and values. An object is delimited by braces, each name is defined between quotation marks followed by a colon and its respective value, and each pair is separated by commas.
An Array is an ordered list of values. An array is delimited by brackets and its values are separated by commas. Values can be numbers or texts, which must be represented between quotation marks. An example of a message in JSON format:
{"s":1, "t":"2014-07-11T15:26:37Z", "q":192,"c":1, "x":-1.234,"y":0.234, "z":-0.234}
To extract the content of the previous message, a possible Template would be the following:
{"s":"DUMMY", "t":"TS_TEXT(%y-%m-%dT%H:%M:%SZ)","q":"QL_OPC", "c":"DUMMY", "x":"E3VAL","y":"E3VAL", "z":"E3VAL"}
In this case, users must create a Block Tag with three Block Elements, and each Block Element receives one of the x, y, and z values, respectively.
The next code contains an example of a message in JSON format:
{
"n_channels":2,
"timestamp":1504198675,
"hash":"1842E0F97392F08BDF996961A8333832AB06D113",
"battery":5.13,
"gmt":-3,
"tag_channels":["Analog1","Analog2"],
"value_channels":[28.100,27.200],
"tag_units":["°C","°C"],
"alarm_low":[0,0],
"alarm_high":[0,0],
"buzzer_state":0
}
A possible Template for the previous example would be the following:
{
"n_channels":"DUMMY",
"timestamp":"TS_UNIX",
"hash":"DUMMY",
"battery":"V1",
"gmt":"V2",
"tag_channels":["V3","V4"],
"value_channels":["V5","V6"],
"tag_units":["V7","V8"],
"alarm_low":["V9","V10"],
"alarm_high":["V11","V12"],
"buzzer_state":"V13"
}
Note: Both message and templates shall be JSON objects or arrays, starting with { or [.
Value/Object Repetition inside Arrays
Arrays can be used to receive a variable number of elements. Let´s take as an example the messages below:
Message 1:
{
"MessageType": "Test",
"TagData": [
{
"Temperature": 11,
"Humidity": 50
},
{
"Temperature": 14,
"Humidity": 80
}
]
}
Message 2:
{
"MessageType": "Test",
"TagData": [
{
"Temperature": 11,
"Humidity": 12
},
{
"Temperature": 14,
"Humidity": 66
}
{
"Temperature": 70,
"Humidity": 55
}
]
}
We can see that inside the array, it is possible to find 1, 2, 3 or N objects containing temperature and humidity, and it might be interesting if a "Test" Block Tag, with 2 elements (Temperature and Humidity) can receive a reading at each object found.
To achieve this, you can declare the template with the keyword Repeat_E3VAL right after the '[' symbol that marks the array start. Example:
{"MessageType":"DUMMY","TagData":[Repeat_E3VAL{"Temperature":"E3VAL","Humidity":"E3VAL"}]}
In this way, when we receive message 2, for example, we will receive a call at the block OnRead event for each object found. Example:
Sub [Sensor-001_OnRead]()
Application.Trace Item("Temperature").Value
Application.Trace Item("Humiddty").Value
End Sub
Log:
910 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 11 14
911 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 12 14
912 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 14 14
913 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 66 14
914 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 70 14
915 2021-08-05 10:06:15.826 0x3FF8 0x3210 APPTRACE 55 14
If more keywords E3VAL were declared at the template before the repetition point, those previous values will be repeated for each set. As an example, if we have declated the template as
{"MessageType":"E3VAL","TagData":[Repeat_E3VAL{"Temperature":"E3VAL","Humidity":"E3VAL"}]}
we could have the block with 3 elements (Type, Temperature and Humidity), so we would receive the following values at each OnRead event:
"Test";11;12 '=> 1st OnRead
"Test";14;66 ' => 2nd OnRead
"Test";70;55 '=> 3rd OnRead