-
Notifications
You must be signed in to change notification settings - Fork 108
JSON on Soletta flows
In Soletta™ Framework, there are two different packet types used exclusively to hold JSON elements: json-object and json-array. Soletta also provides utility nodes to parse, create and handle those packets.
One way of creating a JSON array or object is converting them from a String or Blob packet types. When we read a JSON from a file, from an http connection or from any other source, usually we will read them as a blob packet or a String packet. To use JSON specific nodes, we need to convert them to json-array or json-object packets.
To convert from String to a JSON Object and print it to console:
my_json_string(constant/string:value="{\"my_element\": 123}")
json_object_converter(converter/string-to-json-object)
my_json_string OUT -> IN json_object_converter OUT -> IN my_console(console)
To convert from String to a JSON Array and print it to console:
my_json_string(constant/string:value="[1,2,3]")
json_array_converter(converter/string-to-json-array)
my_json_string OUT -> IN json_array_converter OUT -> IN my_console(console)
To convert from a Blob we read from a file to a JSON Object and print it to console:
json_object_converter(converter/blob-to-json-object)
file_reader(file/reader:path="file.json") OUT -> IN json_object_converter
json_object_converter OUT -> IN my_console(console)
To convert from a Blob we read from a file to a JSON Array and print it to console:
json_array_converter(converter/blob-to-json-array)
file_reader(file/reader:path="file.json") OUT -> IN json_array_converter
json_array_converter OUT -> IN my_console(console)
Now that we have json-object and json-array packets we may want to retrieve information from them. Soletta provide useful nodes for that.
Nodes that get data from JSON Objects or arrays always forward the data to an appropriate output port, with its value converted to correct packet type. Strings are forwarded to the STRING port, boolean values, to BOOLEAN port, numbers to INT and FLOAT ports, JSON Objects to OBJECT port and JSON Arrays to ARRAY port.
To get an int value from from a specific key from a JSON Object and print it to console:
my_json_string(constant/string:value="{\"my_element\": 123}")
json_object_converter(converter/string-to-json-object)
json_get_int(json/object-get-key:key="my_element")
my_json_string OUT -> IN json_object_converter OUT -> IN json_get_int
json_get_int INT -> IN output(console)
To get an int value from from a specific index from a JSON Array and print it to console:
my_json_string(constant/string:value="[1,2,3]")
json_array_converter(converter/string-to-json-array)
json_get_int(json/array-get-at-index:index=0)
my_json_string OUT -> IN json_array_converter OUT -> IN json_get_int
json_get_int INT -> IN output(console)
Complex JSON Objects or Arrays may be difficult to index using json/object-get-key or json/array-get-at-index nodes. We may need to go through multiple layers of objects and arrays until we find the element we are looking for. To make it easier to select elements from a JSON Array or Object, Soletta provide nodes that uses JSON path to index an specific element. More information about JSON path notation can be found at http://goessner.net/articles/JsonPath/
Note: parent, wildcard, union , array slice, filter and script JSON path operators are not supported in Soletta nodes.
To get an int value from from a specific JSON path from a JSON Object and print it to console:
my_json_string(constant/string:value="{\"my_element\": [0, 1, {\"obj\": 456}]}")
json_converter(converter/string-to-json-object)
json_get_int(json/object-get-path:path="$.my_element[2].obj")
my_json_string OUT -> IN json_converter OUT -> IN json_get_int
json_get_int INT -> IN output(console)
To get an int value from from a specific JSON path from a JSON Array and print it to console:
my_json_string(constant/string:value="[{\"my_element\": [0, 1, {\"obj\": 456}]}]")
json_converter(converter/string-to-json-array)
json_get_int(json/array-get-path:path="$[0].my_element[2].obj")
my_json_string OUT -> IN json_converter OUT -> IN json_get_int
json_get_int INT -> IN output(console)
To get the number of children from a JSON Object and print it to console:
my_json_string(constant/string:value="{\"key\": 123,\"key2\": false}")
json_object_converter(converter/string-to-json-object)
json_length(json/object-length)
my_json_string OUT -> IN json_object_converter OUT -> IN json_length
json_length OUT -> IN output(console)
To get the number of elements in a JSON Array and print it to console:
my_json_string(constant/string:value="[1,2,3]")
json_array_converter(converter/string-to-json-array)
json_length(json/array-length)
my_json_string OUT -> IN json_array_converter OUT -> IN json_length
json_length OUT -> IN output(console)
To get all keys from a JSON Object as String and print them to console:
my_json_string(constant/string:value="{\"key\": 123,\"key2\": false}")
json_object_converter(converter/string-to-json-object)
json_get_keys(json/object-get-all-keys)
my_json_string OUT -> IN json_object_converter OUT -> IN json_get_keys
json_get_keys OUT -> IN output(console)
To get all elements from a JSON Array and print them to console:
my_json_string(constant/string:value="[1,2,3]")
json_array_converter(converter/string-to-json-array)
json_get_elements(json/array-get-all-elements)
my_json_string OUT -> IN json_array_converter OUT -> IN json_get_elements
json_get_elements INT -> IN output(console)
In some cases we don't have a JSON object or array, but we want to create one from scratch. We may want that to use it to make an http request for example. Soletta provides us nodes to create JSON Object and Arrays using keys and index or JSON Path syntax as specified in Use JSON Path to index a JSON Object or Array section.
To create a JSON Object from scratch and print it to console:
DECLARE=pair:composed-new:KEY(string)|VALUE(int)
create_json(json/create-object)
_(constant/string:value="key") OUT -> KEY int_pair(pair)
_(constant/int:value=3) OUT -> VALUE int_pair
int_pair OUT -> INT create_json
int_pair OUT -> CREATE create_json
create_json OUT -> IN output(console)
To create a JSON Array from scratch and print it to console:
create_json(json/create-array)
_(constant/int:value=1) OUT -> INT create_json
_(constant/int:value=2) OUT -> INT create_json
_(constant/int:value=3) OUT -> INT create_json
_(constant/empty) OUT -> CREATE create_json
create_json OUT -> IN output(console)
To create a JSON Object from scratch and print it to console, using JSON path:
DECLARE=pair:composed-new:KEY(string)|VALUE(int)
create_json(json/create-object-path)
_(constant/string:value="$.my_element[0].key") OUT -> KEY int_pair(pair)
_(constant/int:value=3) OUT -> VALUE int_pair
int_pair OUT -> INT create_json
int_pair OUT -> CREATE create_json
create_json OUT -> IN output(console)
To create a JSON Array from scratch and print it to console, using JSON path:
DECLARE=pair:composed-new:KEY(string)|VALUE(int)
create_json(json/create-array-path)
_(constant/string:value="$[0].my_element[0].key") OUT -> KEY int_pair(pair)
_(constant/int:value=3) OUT -> VALUE int_pair
int_pair OUT -> INT create_json
int_pair OUT -> CREATE create_json
create_json OUT -> IN output(console)