Javascript origin
JSON originates from the object literals of
JavaScript (ECMAScript) and is specified in
RFC 8259 (The JavaScript Object Notation (JSON) Data Interchange Format) which obsoleted
RFC 7159.
In fact, JSON is referred in ECMA-262 (see also ECMA-404 (The JSON Data Interchange Format)).
Because the JSON syntax originates in JavaScript, it is possible to use
eval(jsonDocument)
in JavaScript to parse a JSON document.
Of course, this poses a
security risk (like that of
SQL injection) if the JSON document contains executable code.
Primitive data types (scalars)
JSON specifies four primitive data types. Each scalar value has one of these data types.
Data Type | Example | Comment |
String | "string" | A sequence of zore or more Unicode characters (UTF-8 (default), UTF-16 or UTF-32). |
Number | 42 , -13.9 | Represented in base 10; leading zeros or plus signs are not allowed. |
Boolean | true , false | The only two values whose data type is boolean. |
Null | null | The only value whose data type is null. |
Strings
RFC-7159 contains the following remarks about strings (especially about using the backslash character which is called the
reverse solidus in the document):
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus [aka backslash], and the control characters (U+0000 through U+001F).
Any character may be escaped. If the character is in the Basic Multilingual Plane […], then it may be represented as a six-character sequence: a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point.
RFC-7159 also says that some characters can be represented with two characters: \n
, \t
, \\
etc.
Thus, the following is a correct JSON string that contains quotes and a
new line:
"He said:\n\"hello\"."
.
JSON does not specify a data-type for dates and/or times.
Dates are generally stored in JSON as strings in the
ISO 8601 format.
This StackOverflow answer recommends to use the format that is returned by JavaScript's method
Date.ToJSON()
, for example
2012-04-23T18:25:43.511Z
.
Structured types
The primitive data types are building blocks to create structured types.
There are two structured types:
objects | Unordered set name/value pairs |
arrays | Ordered list of values (whose data type need not to be the same) |
Objects
JSON objects are embedded in curly braces ({ … }
).
A JSON object consists of comma separated key-value pairs. The key is a string and is separated from the value by a colon (:
).
These key value pairs are referred to as members.
{
"item": "Lemon",
"price": {
"val": 1.84,
"currency: "CHF"
}
}
Arrays
Arrays are embedded in square brackets: [ … ]
.
An array is an ordered, comma-separated list of JSON values.
[ "foo",
42,
null,
{ "foo": "bar" }
]
Pretty printing JSON on the command line
The
jq
command line tool is arguably the best way for pretty printing JSON.
With
Python, the following line would pretty print a JSON object:
echo '{"num": 42, "txt": "Hello world"}' | python -m json.tool