JSON Encode / Decode Processors

The “JSON Encode” and “JSON Decode” processors are installed with the Dataristix Application. The processors take one or more values as inputs and either encode the inputs into a JSON object string value or decode a JSON object string value into individual values for further processing in the task pipeline.

The “JSON Value Properties Encoder” processor takes any input value and encodes it’s value properties. Value properties are generated at the source of data and consist of the source timestamp (UTC), a status code (aligned with the OPC UA status code definition) and the actual value. The encoded string is a JSON object with properties “Value”, “Timestamp”, and “Status”.

See also: Value Properties Decoder Processor.

Add to Task

Add the respective processor to a task by dragging it from the “Build” panel onto the task’s “Task Builder” panel.

../../_images/processor-jsonencodedecode.png

JSON Encoder Configuration

Select the JSON Encoder processor item in the “Task Builder” panel to view its properties.

../../_images/processor-jsonencode-edit.png

You can change the following properties.

  • Processor Topic Name

    You can change the processor topic name to suit. When changing the topic name to something other than “JSON Encoder” then the “JSON” output tag will be renamed to carry the name of the processor topic. This may be helpful when the “JSON” output tag is connected to “generic” output topics that carry the tag name forward.

  • Representative JSON object

    Enter a representative JSON object string into the entry field provided and press “Apply”. The JSON Encoder processor will generate input tag connection points for each of the properties within the JSON object. When tag values are sent to these input tag connection points then the JSON Encoder will generate a “JSON” output string with property values replaced with actual values.

JSON Value Properties Encoder Configuration

Select the JSON Value Properties Encoder item in the “Task Builder” panel to view its properties.

You can change the following properties.

  • Processor Topic Name

    You can change the processor topic name to suit.

JSON Decoder Configuration

Select the JSON Decoder processor item in the “Task Builder” panel to view its properties.

../../_images/processor-jsondecode-edit.png

You can change the following properties.

  • Processor Topic Name

    You can change the processor topic name to suit. This may be helpful when the topic connection point is used to connect to “generic” output topics that carry the topic name forward.

  • Representative JSON object

    Enter either a list of property names (or JSON paths), a representative JSON object string beginning with {, or a JSON array beginning with [ (Dataristix version 1.11073 or later) into the entry field provided and press “Apply”. The JSON Decoder processor will expect any connected input tag to be of type “string”, which needs to be a JSON object containing the entered property names, or a JSON object or array matching the entered representative JSON object.

    When tags are connected to the processor, then output tags are generated for each entered property or for each property within the representative JSON object.

    For JSON top level arrays, a single output tag named “[]” is generated. For array elements of a value type, the output tag receives an array of the corresponding value type. For array elements that are JSON objects, the output tag receives a string array containing the array element objects as JSON strings. Chain additional JSON decoders to decode the JSON string for each element, for example, by utilizing the Array Sequencer Processor (see example at the end of this chapter).

    For arrays embedded within a JSON object, the given JSON property name is used as the output tag name, and the output tag receives the decoded value array or string array as described above.

Examples

Encode JSON

In the following example, signals are received from the OPC UA Connector, then encoded into a JSON object and sent to the MQTT connector.

../../_images/processor-jsonencode-example.png

Encode Value Properties as JSON payloads

In the following example, values are received from the OPC UA Connector. Value properties as received from the OPC UA server are encoded into a JSON payload and then forwarded to the MQTT Connector.

../../_images/processor-jsonvaluepropertiesencoder-example.png

Decode JSON

In the following example, a JSON object string containing Sine signals is received from the MQTT Connector, then decoded into individual values and sent to the CSV connector. Note that you can connect multiple tags to the same JSON Decoder.

../../_images/processor-jsondecode-example.png

Decode JSON Arrays

In this example, the expected JSON payload consists of a JSON object array in the form

[{"elemId":1, elemValue: 10.0},{"elemId":2, elemValue: 20.0},{"elemId":3, elemValue: 30.0}]

If the number of elements within the JSON array is known in advance, then JSON paths may be used to access individual element values for processing. For example, if the number of elements is known to be three, then the following representative JSON object will retrieve all element ID’s and values:

$.[0].elemId
$.[0].elemValue
$.[1].elemId
$.[1].elemValue
$.[2].elemId
$.[2].elemValue

A task can then process the individual tag values as follows:

../../_images/processor-jsondecode-example-array-1.png

If the number of elements within the JSON array is not known in advance, then the array needs to be decoded into a string array containing the JSON strings for the individual objects first. Note that, because property names within elements of the JSON array are not evaluated within the array decoder, the representative JSON object for arrays may also be abbreviated to:

[]

The following task shows the JSON payload being received from the MQTT “ElemArray” input. The JSON decoder then outputs a string array containing the JSON strings for individual elements.

../../_images/processor-jsondecode-example-array-2.png

Each element within the output array is a string, and as such “elemId” and “elemValue” tag values are not directly available for processing. We therefore need a second JSON decoder to decode a representative JSON object in the form:

{
    "elemId": 1,
    "elemValue": 1.0
}

The Array Sequencer Processor is used to sequentially feed string array elements into the decoder and the decoder output yields the desired “elemId” and “elemValue” tag values for each element in the array.

../../_images/processor-jsondecode-example-array-3.png

Note that the “Monitor” output at the end of the task pipeline receives all array elements in sequence, although only the last “elemId” and “elemValue” live values are shown.