We have it from the horse's mouth [Cro08][14]:
JavaScript Object Notation (JSON) is a lightweight interchange format. It is based on JavaScript's object literal notation, one of JavaScript's best parts. Even though it is a subset of JavaScript, it is language independent. It can be used to exchange data between programs written in all modern programming languages. It is a text format, so it is readable by humans and machines. It is easy to implement and easy to use. There is a lot of material about JSON at http://www.JSON.org/.
MDN says
“
JSON is a syntax for serializing
objects, arrays, numbers, strings,
booleans, and null. It is based upon
JavaScript syntax but is distinct from it:
some JavaScript is not JSON.
”
Serializinf means transforming data structures
into a printable text format, a
string.
JSON is from 2001, and
it became an internet standard in 2017.
A programming language implementing JSON
must implement the ellip
JSON.parse("{…}")
method transforming an assumed JSON string
into an object in the implementing language,
and the
JSON.stringify(obj, […])
method creating a JSON string from an object.
In [],
[Crockford, 2008, App. E]
there is a print of a JSON parser.
In interactive node the following creates a JavaScript object, line 4, stringifies it, line 10, and prints it, line 12:
$ node
Welcome to Node.js v19.4.0.
Type ".help" for more information.
> let o = {name: "Aarhus", population: 340421, country: "DNK"};
undefined
> let s = JSON.stringify(o);
undefined
> s
'{"name":"Aarhus","population":340421,"country":"DNK"}'
> s = JSON.stringify(o, null, 4);
'{\n "name": "Aarhus",\n "population": 340421,\n "country": "DNK"\n}'
> console.log(s);
{
"name": "Aarhus",
"population": 340421,
"country": "DNK"
}
undefined
>
In interactive node the following creates a JavaScript object, line 4, stringifies it with some beautification, line 24, and prints it in line 42:
$ node
Welcome to Node.js v19.4.0.
Type ".help" for more information.
> let o = [
... {
... name: "Aarhus",
... population: 340421,
... country: "DNK"
... },
... {
... name: "Fredericia",
... population: 51326,
... country: "DNK"
... },
... {
... name: "Kolding",
... population: 92515,
... country: "DNK"
... }
... ];
undefined
> console.log(JSON.stringify(o, null, 4));
[
{
"name": "Aarhus",
"population": 340421,
"country": "DNK"
},
{
"name": "Fredericia",
"population": 51326,
"country": "DNK"
},
{
"name": "Kolding",
"population": 92515,
"country": "DNK"
}
]
undefined
>
JSON objects are derived from JavaScript objects with a few tightening rules. A JSON object is a curly braces delimited, unordered, and comma-separated list of name/value pairs. There may be between 0 and many elements. The comma must be omitted after the last element. The rules are:
true
false
, ornull
What is missing compared to JavaScript values?
The lines 4, 6, and 8 in the interactive node below illustrates an array with plain string elements:
$ node
Welcome to Node.js v16.13.2.
Type ".help" for more information.
let o = ["DNK", "NOR", "SWE"]
[ 'DNK', 'NOR', 'SWE' ]
> let s = JSON.stringify(o, null, 2);
'[\n "DNK",\n "NOR",\n "SWE"\n]'
> console.log(s);
[
"DNK",
"NOR",
"SWE"
]
undefined
A JSON array is a ordered, and comma separated list of values. The values must be of the types mentioned above[16].
[15] Do, however think of the host systems using the object. Names become attributes or properties. It might therefore be a good rule of thumb to follow the rules of variable names from host programming languages.
[16] It is syntactically legal to mix types in an array. It might, depending on usage, be a good rule of thumb not to. Some host programming languages do not accept it.