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 file has file name extension
.json
. - JSON format is based on JavaScript objects syntax, of object/array arbitrarily nested.
JSON Code Example
{ "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 }
Difference Between JavaScript Syntax vs JSON Syntax
JSON syntax is more strict than JavaScript object/array syntax.
String key must be double quoted
- Good:
{"h":2}
- Bad:
{h:2}
- Bad:
{'h':2}
JSON does not allow extra comma at end.
- good:
{"h":2}
- bad:
{"h":2,}
JSON does not allow colon at the end.
- good:
{"h":2}
- bad:
{"h":2};
JSON value does not allow undefined
.
(but null
is ok)
- good:
{"h":null}
- bad:
{"h":undefined}