JavaScript: JSON
JSON
is the value of the property key "JSON"
of
the Global Object.
console.log( window["JSON"] === JSON ); // true
Type
console.log( typeof JSON === "object" ); // true console.log( Object.prototype.toString.call( JSON ) === "[object JSON]" ) // true
Parent
Parent of JSON
is Object.prototype.
[see Prototype and Inheritance]
console.log( Reflect.getPrototypeOf ( JSON ) === Object.prototype ); // true
Purpose
Purpose of JSON
is as a namespace, for functions working with the JSON data interchange format.
What is JSON Data Interchange Format?
JSON is a data interchange format.
JSON is a more strict syntax of nested JavaScript object or array. Used to pass data to the web browser, or exchange data with other language, API.
For example, here's a JSON:
{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" } ], "children": [], "spouse": null }
JavaScript Syntax vs JSON Syntax
JSON syntax is more strict than JavaScript syntax. For example:
{h:2}
- bad. JSON does not allow unquoted property key.
{'h':2}
- bad. JSON does not allow single quote.
{"h":2,}
- bad. JSON does not allow extra comma at end.
[undefined]
- bad. JSON value does not allow
undefined
. (butnull
is ok)