Skip to content

JSON Type (realvirtual Dialect)

Commercial

Included in the public demo Compare editions

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.

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 VAR and VAR_TEMP, as inputs, outputs and in-outs of your own function blocks, and as input and result of a function.
  • 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, of or do, is written as a string key: order['to']. The form order.to is 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_STRING and JSON_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.
  • 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 of b does not change a; there are no references. Copying a large value takes time on every scan.

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).

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.

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 OrderCheck
VAR_EXTERNAL
OrderText : STRING(1000);
PartId : DINT;
Lines : INT;
Accepted : BOOL;
END_VAR
VAR
order : JSON := '{"items":[]}';
key : STRING := 'qty';
ok : BOOL;
END_VAR
order := 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_PROGRAM

With this text in OrderText:

{"items":[{"qty":2}],"draft":true}

the part gets the document:

{"history":["checked"],"items":[{"qty":3}],"to":"Station2"}
  • VAR_GLOBAL with type JSON: globals are signals, and there is no JSON signal type.
  • JSON in VAR_EXTERNAL or with AT, 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.