JSON Data Format
What is JSON
- JSON is a data interchange format. Used to pass data to the web browser, or exchange data with other language, API, or used as a software config file.
- JSON file is plain text file.
- JSON format is based on JavaScript objects syntax, of object/array arbitrarily nested.
JSON file name extension
JSON file has file name extension .json.
Special values in JSON
json has the following special literal values
truefalsenull
JSON Code Example
Simple array
[1, 6.9, "abc"]
Simple array with special values
[1, 6.9, "abc", true, false, null]
Simple object
{ "joe": 42, "mary": 20, "jenny": 25 }
Nested array and object
{ "name": "Joe", "alive": true, "age": 25, "address": { "street": "1st Street", "state": "Cali" }, "phone": [ { "type": "home", "number": "123 456 7890" }, { "type": "office", "number": "123 456 7890" } ], "children": [], "spouse": null }
Difference Between JavaScript Syntax vs JSON Syntax
JSON syntax is more strict than JavaScript object/array syntax.
Key must be double quoted
// good {"h":2} // bad // {h:2} // bad // {'h':2}
Does not allow extra comma at end
// good {"h":2} // bad // {"h":2,}
Does not allow semicolon at the end
// good {"h":2} // bad // {"h":2};
Does not allow undefined
JSON value does not allow undefined.
(but null is ok)
// good {"h":null} // bad // {"h":undefined}
Does not allow comment
JSON does not allow comment in JSON string or file. Some language may support it, some not.