What Is JSON? A Practical Introduction
JSON (JavaScript Object Notation) is a plain-text format for structured data. It is the default format for web APIs, a common choice for configuration files, and the format many logging systems write. This guide explains what JSON looks like, why it became so widely used, and the handful of rules that cause most errors.
What JSON looks like
A JSON document describes one value. Most of the time that value is an object, a set of key-value pairs in curly braces, or an array, an ordered list in square brackets. Values can be nested inside each other to any depth.
{
"id": 1024,
"name": "Ada Lovelace",
"active": true,
"languages": ["English", "French"],
"address": { "city": "London", "postcode": null }
}That example uses every kind of JSON value: a number, strings, a boolean, an array, a nested object and null.
Where JSON came from
JSON was popularised in the early 2000s as a lighter alternative to XML for sending data between browsers and servers. Its syntax is a subset of JavaScript object literals, which made it trivial to use in web pages. Today it is defined independently of JavaScript by two matching standards, RFC 8259 from the IETF and ECMA-404, and every mainstream programming language has a JSON parser in its standard library or a widely used package.
Why JSON is so widely used
- It is easy to read. A person can scan a JSON response and understand it without documentation.
- It maps directly to code. Objects become dictionaries, maps or structs; arrays become lists.
- The grammar is tiny. The whole specification fits on a few pages, so parsers are fast and consistent.
- It is text. It travels over HTTP, sits in files and can be stored in databases without special handling.
Where you will meet JSON
- Web APIs: request and response bodies, usually with the
Content-Type: application/jsonheader. - Configuration: files such as
package.json,tsconfig.jsonand editor settings. - Logs and events: structured logs are often written as one JSON object per line (NDJSON).
- Databases: document databases store JSON-like documents, and many SQL databases have JSON column types.
The rules that cause most errors
JSON is strict, and most "invalid JSON" errors come from habits carried over from JavaScript or Python:
- Keys must be strings in double quotes:
{"a": 1}, not{a: 1}or{'a': 1}. - Strings must use double quotes too.
- No trailing commas after the last item in an object or array.
- No comments. Neither
//nor/* */is allowed. undefined,NaNandInfinityare not JSON values.
{
"name": 'Ada', // single quotes and a comment: invalid
"tags": ["a", "b",], // trailing comma: invalid
}Paste broken JSON into the JSON Validator to see the line and column of the first problem.
JSON compared with other formats
XML can express everything JSON can but is more verbose and has no built-in data types; see JSON vs XML. YAML is easier to write by hand and supports comments, which is why it is popular for configuration; see YAML basics. JSON5 relaxes JSON's rules for hand-edited files; see JSON vs JSON5.
Frequently asked questions
Is JSON only for JavaScript?
No. It started with JavaScript syntax but is language-independent, and every major language can read and write it.
What file extension does JSON use?
.json, and the media type is application/json.
Can a JSON document be just a number or a string?
Yes. RFC 8259 allows any JSON value at the top level, although objects and arrays are by far the most common.
Is JSON a programming language?
No. It is a data format. It describes data but cannot contain logic or functions.