JSONDecoder
JSONDecoder
The JSONDecoder is a sequencer block, that allows decoding input event data from JavaScript Object Notation (JSON) format to output events with decoded data.
Events
Name | Appearance | Description |
---|---|---|
Decode | Predefined input event | Invokes JSON message decoding from EncodedMessage argument. User-added events and Decoded event will be emitted for each decoded argument. |
Events with JSON array attribute name | Any number of user-configurable output events for emitting array elements | JSON array attribute to decode from input and send its elements one by one via this event (before Decoded event will be emitted). |
Events, that list Data arguments having names specified in DynamicKey or DynamicValue properties | Any number of user-configurable output events for emitting dynamic key-value pairs | The event will be emitted each time a JSON object contains a simple key-value where the object's key does not have a the corresponding argument in the configuration of the block, DynamicKey or DynamicValue have both been set and there is an Event that lists at least one of the arguments set in DynamicKey or DynamicValue. (All these events will be emitted before Decoded event will be emitted). |
Decoded | Predefined output event | Will be emitted on Decode event for each argument listed in its Data property and contains values of decoded JSON message attributes as arguments, that are attached to the event via Data property |
Arguments
Name | Appearance | Description |
---|---|---|
EncodedMessage | Predefined input argument | The JSON encoded message. Is by default included with Decode event. |
JSON attribute name | Any number of user-configurable output arguments | JSON attribute to decode from input and send the argument together with output events |
Example
The following block diagram shows how to cascade two JSONDecode blocks to decode a JSON string like this:
{ "device_id": 1, "measurements": [ { "temp": 25.6, "humidity": 55 }, { "temp": 24.5, "humidity": 56 } ] }
By adding an output event named measurements to the first JSONDecoder, a JSON array attribute named measurements will be searched from the input JSON and all its elements will be emitted with the measurements event to the cascaded JSONDecoder1. JSONDecoder1 has arguments temp and humidity added, to instruct the block to search for these attributes in input JSON and when found, assign their values as argument values emitted together with Decoded event.
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 the example JSON, their output will look something like this:
20:14:47.086 DEBUG: App.InputDebug: EventIn: Data arguments: EncodedMessage= { "device_id": 1, "measurements": [ { "temp": 25.6, "humidity": 55 }, { "temp": 24.5, "humidity": 56 } ] } 20:14:47.086 DEBUG: App.ArrayElementDebug: EventIn: Data arguments: EncodedMessage={"temp":2.56E1,"humidity":55} 20:14:47.086 DEBUG: App.ArrayElementDebug: EventIn: Data arguments: EncodedMessage={"temp":2.45E1,"humidity":56} 20:14:47.086 DEBUG: App.ToplevelDebug: EventIn: Data arguments: device_id=1 20:14:47.086 DEBUG: App.ArrayElementObjectDebug: EventIn: Data arguments: temp=25.600000381469726562;humidity=55 20:14:47.086 DEBUG: App.ArrayElementObjectDebug: EventIn: Data arguments: temp=24.5;humidity=56
See also JSONEncoder, JSONDecode, 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.