JSONEncoder
JSONEncoder
The JSONEncoder is a sequencer block, that allows to encode input events data to JavaScript Object Notation (JSON) format as output event with encoded data.
Events
Name | Appearance | Description |
---|---|---|
Events with JSON array attribute name | Any number of user-configurable input events for array element data | Data of this input event will be appended to the JSON message array attribute named as the event |
Events, that list Data arguments having names specified in DynamicKey or DynamicValue properties | Any number of user-configurable input events for appending dynamic key-value pairs to the JSON message. | Data arguments that are also listed in DynamicKey and DynamicValue will be used to encode one key-value pair into the JSON message for each time the event is recieved before Encode message is recieved. Any other non dynamic arguments listed in the events Data property will add (or update) an attribute key-value pair defined by the argument's name and value in the JSON message. |
Encode | Predefined input event | Causes gathered data to be encoded. Event Data arguments will be added and encoded as JSON message attributes |
Encoded | Predefined output event | Will be emitted on Encode event and contains encoded JSON message in EncodedMessage argument. |
Init | Predefined input event | Will clear all gathered data and reset the encoder, then will emit Inited |
Inited | Predefined output event | Will be emitted after Init is done |
Arguments
Name | Appearance | Description |
---|---|---|
JSON attribute name | Any number of user-configurable input arguments | Argument name and value will be encoded to output as JSON attribute name and value |
EncodedMessage | Predefined output argument | The encoded JSON message. Will be sent out with Encoded events. |
Example
The following block diagram shows how to cascade two JSONEncode blocks to encode a JSON string like this:
{ "device_id": 1, "measurements": [ { "temp": 25.6, "humidity": 55 }, { "temp": 24.5, "humidity": 56 } ] }
For that, input arguments temp and humidity were added to the first JSONEncoder, to encode measurement array element JSON objects. These JSON objects are sent to the cascaded JSONEncoder1, into the added input event measurements, that will receive and gather these JSON objects into memory. When Encode event is received the gathered data is encoded with the measurements JSON array included.
To better understand the data flow, some Sequencer.DebugPrinter blocks were also connected to the blocks to print into the application output window the time and the data received with events. For example JSON, their output will look something like this:
20:57:09.285 DEBUG: App.ArrayElementDebug: EventIn: Data arguments: temp=25.600000000000001421;humidity=55 20:57:09.285 DEBUG: App.ArrayElementObjectDebug: EventIn: All arguments: EncodedMessage='{\"temp\":2.56E1,\"humidity\":5.5E1}' 20:57:26.284 DEBUG: App.ArrayElementDebug: EventIn: Data arguments: temp=24.5;humidity=56 20:57:26.285 DEBUG: App.ArrayElementObjectDebug: EventIn: All arguments: EncodedMessage='{\"temp\":2.45E1,\"humidity\":5.6E1}' 20:57:28.583 DEBUG: App.EncodeDebug: EventIn: Data arguments: device_id=1 20:57:28.583 DEBUG: App.EncoedJSONDebug: EventIn: Data arguments: EncodedMessage={"measurements":[{"temp":2.56E1,"humidity":5.5E1},{"temp":2.45E1,"humidity":5.6E1}],"device_id":1}
See also JSONDecoder, JSONEncode, and Argument.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.