An invention from the JavaScript world has become a universal language for exchanging data: browsers, servers and databases written in different languages all understand it in the same way. The syntax is fixed by two specifications, ECMA-404 and RFC 8259.
Syntax
JSON has six structural characters: curly and square brackets, the colon and the comma. They build two kinds of container: an object, which holds “name: value” pairs in curly brackets, and an array, which holds a list of values in square brackets. A value can be a string, a number, true, false, null, or again an object or array.
| Character | Role |
|---|---|
| { } | start and end of an object |
| [ ] | start and end of an array |
| : | separates a name from its value |
| , | separates pairs and array elements |
{ "sku": "A-17", "price": 24.9, "in_stock": true, "tags": ["cables", "usb"], "dimensions": {"width": 12, "height": 3} } Names within an object should be unique: with duplicates, parser behaviour is unpredictable — many return only the last value, others report an error. The order of pairs in an object is not guaranteed, while array elements are ordered.
Text, not code
JSON describes data only: unlike JavaScript it has no assignment or calls, and unlike XML it has no opening tags or comments. Numbers are written in decimal, without leading zeros; infinity and NaN cannot be expressed. Strings go in double quotes, with the quote, the backslash and control characters escaped; any Unicode character is allowed inside.
JSON text is transferred as UTF-8, and its media type is application/json. A separate specification, JSON Schema, describes what a document must look like — required fields, types, value ranges. The “one line of the file, one object” format is known as JSON Lines and is common for logs and streamed data delivery.
Where it is used
- exchange between client and server: request and response bodies in HTTP APIs;
- configuration and description files:
package.json,tsconfig.json, OpenAPI specifications; - data storage and transfer: the
jsonandjsonbtypes in PostgreSQL, service logs.
The standard tools of almost every platform can turn language values into JSON text and back: json in Python, JSON.stringify() and JSON.parse() in JavaScript, encoding/json in Go, Codable in Swift.
Sources
the term is written from these pagesVerified 9 Nov