JSON stands for JavaScript Object Notation. It is a string syntax for describing an arbitrarily “interesting” piece of data. The core “types” that JSON can describe are:
{ "num": 5 }
)[1, 2, 3]
)"kitty"
)true
or false
(case matters)1.23
) or in scientific notation (23E3
which is equivalent to 23000
)Let’s begin with a full example to set our mental model:
{
"num": 12.3,
"str" "kitty",
"bool": true,
"arr": [1,2,3],
"obj": {
"inner_str": "puppy"
}
}
There are a couple of important bits to address here. First, whitespace between the primary delimiters is generally allowed. We could have written the above JSON object on a single line, however it would be arguably much less readable without the line breaks. Next, observe that depth and hierarchy is allowed (we have an outer object which has an element “obj” within that itself is an object). We could have arrays of objects and objects containing arrays. Even arrays are allowed to mix types in JSON. For example, the following is a valid JSON array: [2, "kitty", false]
. JSON types are fully composable in that regard.
Some characters are treated with special meaning in JSON. For example, the "
(double-quote) character is used to define a string. But, what if we want to include a "
character in a string? Let’s take a look at an exmaple below:
{
"str": "The kitty said \"Meow!\" to me."
}
Notice the use of \"
instead of simply "
around our Meow!
. We must do this in order to tell JSON that we would like a explicit "
characters in our string and we don’t mean them to be treated as delimiters like the outer wrapping "
characters. We must do this with all JSON special characters when we wish to include them in strings. We call this “escaping” or “slashing” the JSON string, and the “slashed” versions of these characters we often call the “slash code” or “escape code” for the characters.
The following is a list of the JSON special character “slash codes” with their meaning / character equivalents:
\b
specifies a backspace character\t
specifies a tab character\n
specifies a linefeed character\f
specifies a form feed character\r
specifies a carriage return character\"
specifies a double-quote character\\
specifies a backslash character\uXXXX
specifies a unicode characeter where XXXX is the two-byte hex representation of the desired unicode character