Parsing JSON Data in Codesys is quite an important operation especially when exchanging data over the REST API. The data sent over the REST API is usually sent in JSON format. This means that data is packaged(serialized) in a JSON string at the source and sent to the requesting party which must then deserialize(unpackage) the data to extract the information.
The PRO JSON library
In Codesys, JSON data can de parsed or deserialized using the PRO JSON library developed by tvm that can be downloaded from the Codesys Forge website. To extract information from a JSON string, we use the JSON_TO_STRUCT function block which is part of the library. The function takes in as its inputs, the JSON string to deserialize, the size of the string and a structure of variables of type JSONVAR
JSONVARs
A JSONVAR represents each data value in the JSON string. So during the parsing process, each data value will be extracted and stored in a variable of type JSONVAR. Now, depending on the type of the value, a special property representing the value type will be called in order to access the value
The JSON_TO_STRUCT function block
The main input parameters of the function bock are:
- Execute – Triggers the deserialisation process
- JSONString – Pointer to JSON String to be deserialized
- JSONVars – Pointer to Structure of JSONVARs each reperesenting each value in the JSON string
When the String has successfully been deserialized, the output Done parameter will be set to TRUE
![](https://brightersidetech.com/wp-content/uploads/2025/01/image.png)
Example
String to deserialize
{
"temp": 22.1531,
"humid": 39,
"running": false,
"name": "Motor 1"
}
Create Structure of JSONVARs
As you can clearly see from the JSON string, we have 4 parameters. So we should create a Structure of JSONVARs with 4 variables each representing each parameter in the JSON string
TYPE MOTOR :
STRUCT
temp : JSONVAR;
humid : JSONVAR;
running : JSONVAR;
name : JSONVAR;
END_STRUCT
END_TYPE
Declare variables
In a new program unit, we now create all the variables and Function Block Instances we need for this action
PROGRAM PLC_PRG
VAR
ParseJSON : JSON_TO_STRUCT; // FB Instance
sJsonString : STRING(1000); //json string
MOTOR_DATA : MOTOR; // struct of json vars (humid, temp running, name)
// data variable
temp : REAL;
humid : DINT;
running : BOOL;
END_VAR
Define Function block instance parameters and access deserialized data
ParseJSON(Execute :=,
JSONString:= ADR(sJsonString),
JSONStringSize := SIZEOF(sJsonString),
JSONVars := ADR(MOTOR_DATA_LIST),
NumberOfVars := SIZEOF(MOTOR_DATA_LIST) / SIZEOF(JSONVAR)
);
IF ParseJSON.Done THEN
temp := MOTOR_DATA.temp.Number;
humid := MOTOR_DATA.humid.Integer;
running := MOTOR_DATA.running.Boolean;
END_IF
As you can see, we have created a Function Block instance of type JSON_TO_STRUCT called ParseJSON and we have defined the instance parameters as required. To start the process of parsing the JSON string, we set the Execute parameter to TRUE and wait unitil the Done parameter is set.
We can then access the data by accessing the various properties representing each value type, namely, Number for floats/ Real, Integer for int and Boolean for Booleans.