Overview
Section titled “Overview”JSON is a data type for dynamic data in Structured Text: objects whose keys are known only at run time, lists of changing length, the whole data document of a part. It is an extension of IEC 61131-3 by realvirtual, the realvirtual dialect.
There is no switch to turn the type on: JSON compiles in every program. The editor shows an info marker at every JSON declaration: JSON is a realvirtual type, not IEC 61131-3: not portable to CODESYS/TwinCAT.
Declare a JSON variable
Section titled “Declare a JSON variable”VAR order : JSON := '{"items":[]}'; extra : JSON;END_VAR- Without a start value the variable holds the JSON value null.
- A start value is a JSON text in a STRING literal. It is checked when you deploy.
- JSON variables are allowed in
VARandVAR_TEMP, as inputs, outputs and in-outs of your own function blocks, and as input and result of a function.
Read values
Section titled “Read values”- Field:
order.customer. List element:order.items[i], with an integer expression, starting at 0. Key from a STRING:order[key]. You can chain them:order.items[i][key]. - A key that is an ST keyword, such as
to,by,ofordo, is written as a string key:order['to']. The formorder.tois a syntax error. - Reading gives a JSON value. Convert it before you use it as a number, text or boolean:
JSON_TO_INT,JSON_TO_DINT,JSON_TO_REAL,JSON_TO_LREAL,JSON_TO_BOOL,JSON_TO_STRING. A missing path reads null. Converting null or a value of another kind gives 0, FALSE or empty text, with one warning per call site. - Check what a value is with
JSON_IS_NULL,JSON_IS_OBJECT,JSON_IS_ARRAY,JSON_IS_NUMBER,JSON_IS_STRINGandJSON_IS_BOOL.JSON_LEN(value)counts list elements or object keys.JSON_HAS(value, key)tells whether a key exists.JSON_KEY(value, index)gives the keys of an object in sorted order, for loops. =and<>compare two JSON values by their content. Other operators work on converted values.
Write values
Section titled “Write values”order.items[0].sku := 'A';writes BOOL, integer, REAL, LREAL, STRING and JSON values without a conversion. Missing objects on the way are created; a list grows by one element at its end.JSON_APPEND(target, item)adds an element to a list,JSON_DELETE(target, keyOrIndex)removes a key or an element. Both return TRUE when something changed. ST has no function call as a statement, so assign the result:ok := JSON_APPEND(order.items, 7);.a := b;copies the value. A later change ofbdoes not changea; there are no references. Copying a large value takes time on every scan.
Limits
Section titled “Limits”JSON variables have the limits of MU data: 4096 bytes of JSON text, nesting depth 8 and lists of up to 256 elements. A write that would exceed a limit leaves the value unchanged, and the Problems panel shows one entry for that call (plc-json).
Text signals and whole MU documents
Section titled “Text signals and whole MU documents”There is no JSON signal type. JSON travels between components as text in a STRING signal:
JSON_PARSE(text)turns a JSON text into a JSON value. Invalid text gives null and a warning. The same text at the same place is parsed only once.JSON_TEXT(value)returns the JSON text of a value, in the canonical form of MU data.MU_READ(mu)reads the whole MU data document of a part.MU_WRITE(mu, value)replaces it under the same rules as every other writer and returns FALSE when the write is refused.
Example
Section titled “Example”The program reads an order as text from a signal, raises the quantity of the first item, sets a target, logs the check and writes the order to the part:
PROGRAM OrderCheckVAR_EXTERNAL OrderText : STRING(1000); PartId : DINT; Lines : INT; Accepted : BOOL;END_VARVAR order : JSON := '{"items":[]}'; key : STRING := 'qty'; ok : BOOL;END_VARorder := JSON_PARSE(OrderText);Lines := JSON_LEN(order.items);IF Lines > 0 THEN order.items[0][key] := JSON_TO_INT(order.items[0][key]) + 1; order['to'] := 'Station2'; ok := JSON_APPEND(order.history, 'checked'); ok := JSON_DELETE(order, 'draft'); Accepted := MU_WRITE(PartId, order);END_IF;END_PROGRAMWith this text in OrderText:
{"items":[{"qty":2}],"draft":true}the part gets the document:
{"history":["checked"],"items":[{"qty":3}],"to":"Station2"}Not supported
Section titled “Not supported”VAR_GLOBALwith typeJSON: globals are signals, and there is no JSON signal type.- JSON in
VAR_EXTERNALor withAT, constant JSON variables and arrays of JSON. - A JSON path as the target of a function block output (
=>). Assign the output to a variable first.
JSON variables are part of the program’s memory. They are included in every DES snapshot, and a run continues identically after a restore.
The DES check treats a JSON variable like an array: j.n := JSON_TO_INT(j.n) + 1; without an edge, a timer or a step change is D1 and blocks the run. A scan that leaves a JSON value unchanged does not count as a change, because the check compares the content. MU_READ and MU_WRITE work in DES on the MU data of the run.