# JSON essentials
JSON is the lingua franca of the modern web. Almost all the modern services speak JSON "natively", thus it's very important to be able to produce JSON data in the exact shape and form required.
# Basic types
JSON only have a handful of data types:
- string, e.g.
"Some Text"
- number, e.g.
42
- boolean,
true
orfalse
- null, indicates the absence of value
- array, a list containing other JSON values, enclosed in square brackets, e.g.
[1, 2, "some text", true]
- object, a series of key-value pairs group together, enclosed in curly braces, e.g.
{ "name": "Joe", age: 42 }
NodeScript has a node for each of these.
Assignments:
- select an Array node, click + or × to add/remove elements
- connect the rest of the values into the Object node, observe how the output of the Object changes
- add/remove object keys
- try expanding the results of Array and Object
# Type conversion
As you might've noticed, each node input has a specific type designated by the color of the "dot" (we refer to those as sockets).
So for example, String node accepts a value of type string
, so you can use it to quickly convert other data type into a string.
Here, when number 42
is connected to a String, the value becomes "42"
.
The same works in reverse: "42"
string literal becomes a number 42
when the nodes are connected the other way around.
Obviously, not all the strings can be converted to number, so the value "Not a number"
produced an error when plugged into a Number node.
Since each node knows what data type it expects, most of the time explicit type conversion is not required. However, in some cases it might be necessary (for example, some APIs may do strict data type checks).
Assignments:
- Add a Boolean node by Right-clicking on the canvas and typing
Boolean
. - Connect the result of a Boolean into a String and see if it works.
- Try connecting them the other way around.
# Any type
A lot of nodes will have a gray input socket, indicating that it would accept any
type.
One such example is an Array node. It has a single input of type array
, but each item inside the array has type any
.
When the value is plugged into a socket with any
type, no type conversion occurs. So both "42"
and 42
stay exactly as they are.
However, for convenience, the values you type directly into the text fields of type any
are auto-converted to the most appropriate type.
Assignments:
- expand the result of the Array node, note the data type of each array item
# Nested objects and arrays
JSON objects and arrays are compound types, meaning they can be composed of other data types, including other objects and arrays. Objects occurring within other objects are often referred to as "nested".
Simple nested objects can also be composed with just an Object node by using .
in the key.
# Accessing values
It is oftentimes useful to access individual elements of the nested objects.
There are two ways to do it:
- Simply expand the node output and "drag out" the value of interest — this will create an additional output as is seen below.
- Use Object / Get node and specify the
key
that you want to extract.