What Is JSON and How Does It Work?
3 min read · Published
Table of contents
JSON (JavaScript Object Notation) is a lightweight text format for storing and exchanging structured data. It is the default language of web APIs, configuration files and many databases because it is easy for people to read and trivial for programs to parse.
Despite the name, JSON is language-independent. Python, Java, Go, PHP, C# and virtually every other language ship a JSON parser. It was popularised by Douglas Crockford in the early 2000s and is now specified in RFC 8259 and ECMA-404.
A first example
{
"name": "Ada Lovelace",
"born": 1815,
"languages": ["English", "French"],
"address": { "city": "London", "country": "UK" },
"active": false,
"spouse": null
}
That one object shows every structural idea in JSON: key/value pairs inside braces, an array in square brackets, a nested object, a boolean and null.
The six JSON data types
| Type | Example | Notes |
|---|---|---|
| String | "hello" |
Double quotes only. Escape with \", \\, \n, \uXXXX. |
| Number | 42, -3.14, 1e6 |
No leading zeros, no NaN, no Infinity, no hex. |
| Boolean | true, false |
Lowercase only. |
| Null | null |
Represents "no value". |
| Array | [1, "two", false] |
Ordered, may mix types. |
| Object | {"key": "value"} |
Unordered key/value pairs; keys are strings. |
Dates are not a JSON type. Systems usually store them as ISO 8601 strings ("2026-11-15T10:30:00Z") or as Unix timestamps; see Understanding Unix Timestamps.
Syntax rules that trip people up
- Keys must be double-quoted.
{name: "Ada"}is JavaScript, not JSON. - No trailing commas.
[1, 2, 3,]is invalid. - No comments. Neither
//nor/* */is allowed. - Strings use double quotes.
'single'is invalid. - Top-level value can be anything. A document may be just
42or"text", though objects and arrays are most common.
Not sure whether your document is valid? Paste it into the JSON Validator to get the exact line and column of the first error.
Reading and writing JSON in code
JavaScript has two built-ins, JSON.parse and JSON.stringify:
const text = '{"id": 7, "tags": ["a", "b"]}';
const obj = JSON.parse(text); // text -> object
console.log(obj.tags[1]); // "b"
const out = JSON.stringify(obj, null, 2); // object -> pretty text
Python uses the json module:
import json
data = json.loads('{"id": 7, "tags": ["a", "b"]}')
print(data["tags"][1]) # b
print(json.dumps(data, indent=2))
Precision and size gotchas
JavaScript numbers are 64-bit floats, so integers above 2^53 (9,007,199,254,740,992) lose precision after JSON.parse. APIs that return large IDs, such as 64-bit database keys, often send them as strings for this reason.
JSON is verbose compared with binary formats such as Protocol Buffers, but it compresses well with gzip or Brotli, and "minified" JSON (no extra whitespace) is the normal wire format. Use the JSON Formatter to switch between minified and readable forms, and see How to Format JSON Online for a workflow.
JSON versus similar formats
- XML supports attributes, namespaces and schemas but is heavier; convert with the JSON to XML converter.
- CSV is flat and tabular; flatten arrays of objects with JSON to CSV and go back with CSV to JSON.
- YAML is a superset-like, whitespace-sensitive format popular for configuration.
- JSON5 / JSONC allow comments and trailing commas but are not valid standard JSON.
Where you will meet JSON
REST and GraphQL APIs, package.json, tsconfig.json, browser storage, log lines, NoSQL document stores (MongoDB, CouchDB), PostgreSQL's jsonb columns and JWT payloads. For the API angle, read REST API Basics.
Related tools
JSON Formatter
Format, beautify and minify JSON with custom indentation.
JSON Validator
Check JSON syntax and see the exact error position.
JSON to CSV Converter
Convert arrays of JSON objects to CSV, flattening nested data.
FAQ
Is JSON a programming language?
No. JSON is a text-based data format. It describes data but cannot contain logic or run code.
What is the difference between JSON and a JavaScript object?
JSON is a strict text format: keys must be double-quoted strings and values are limited to six types. JavaScript object literals allow unquoted keys, single quotes, comments, functions and trailing commas.
Can JSON have comments?
No. Standard JSON does not support comments. Some tools accept JSONC or JSON5, but those are different formats.
Keep reading
- How to Format JSON Online (and Fix Common Errors)
Step-by-step guide to formatting, beautifying and minifying JSON online, plus how to read and fix the most common JSON syntax errors.
- REST API Basics
Understand REST APIs: resources, HTTP methods, status codes, JSON payloads, idempotency, pagination, versioning and authentication basics.