Bookmarks

JSON — JSON

JavaScript Object Notation

A text data-serialisation format: six structural characters build objects and arrays, and values are strings, numbers, true, false and null.

Also: JSON file, JSON document, JSON

checked 9 Nov

All terms

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.

CharacterRole
{ }start and end of an object
[ ]start and end of an array
:separates a name from its value
,separates pairs and array elements
jsonan object with a string, a number, a boolean, an array and a nested object
            {              "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 json and jsonb types 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 pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • YAMLYAML

    JSON is a format for exchanging data between programs; YAML describes configurations and supports comments, anchors and several documents in one file.

  • XMLExtensible Markup Language

    JSON describes data only; XML is a markup language with tags, attributes, mixed content, namespaces and schemas.

  • JWTJSON Web Token

    JSON is text holding data; a JWT is the same kind of JSON data, encoded as Base64url and signed or encrypted, and often serves as an access token.

In depth in the references

where the topic is covered in detail