What Is JSON? A Simple Guide with Examples
JSON — short for JavaScript Object Notation — is a lightweight, text-based format for storing and exchanging structured data. It is the most widely used data interchange format on the web today: when your browser fetches a weather update, submits a login form, or loads a social feed, the server almost certainly responds with JSON. Despite its name, JSON is language-independent and is supported natively or through libraries in virtually every programming language.
The shortest possible definition
JSON is a way to represent structured data as plain text, using key-value pairs for objects and comma-separated values inside square brackets for arrays. A valid JSON document is either an object ({}), an array ([]), or a single primitive value. Every JSON file can be opened and read in a plain-text editor, which makes it easy to inspect, debug, and share.
Here is a minimal JSON object representing a user:
{
"name": "Alice",
"age": 30,
"verified": true,
"email": "alice@example.com"
}
Each entry is a key (always a double-quoted string) followed by a colon, then a value. Multiple entries are separated by commas. The whole thing is wrapped in curly braces.
The six value types JSON supports
JSON is deliberately minimal. It defines exactly six types of values:
| Type | Example | Notes |
|---|---|---|
| String | "hello" | Must use double quotes — single quotes are not valid JSON |
| Number | 42, 3.14, -7 | Integers and decimals; no octal or hex literals |
| Boolean | true, false | Lowercase only |
| Null | null | Represents an intentionally absent value |
| Object | {"key": "value"} | Unordered collection of key-value pairs |
| Array | [1, "two", true] | Ordered list; values may be mixed types |
Objects and arrays can nest inside each other to any depth, which is what makes JSON expressive enough to describe complex real-world data.
A realistic example: nested JSON
Real API responses contain nested objects and arrays. Here is a JSON payload representing an order in an e-commerce system:
{
"orderId": "ORD-8821",
"status": "shipped",
"customer": {
"id": 4091,
"name": "Bob Chen",
"email": "bob@example.com"
},
"items": [
{"sku": "HEADPH-01", "qty": 1, "unitPrice": 79.99},
{"sku": "CABLE-USB-C", "qty": 2, "unitPrice": 8.50}
],
"shippedAt": "2026-07-03T14:22:00Z"
}
The top-level object has string values ("orderId", "status"), a nested object ("customer"), an array of objects ("items"), and a datetime string. JSON has no dedicated date type — dates are conventionally stored as ISO 8601 strings and parsed by the application.
The rules: what makes JSON valid
JSON has a strict grammar. The rules that trip people up most often:
- Keys must be double-quoted strings.
{name: "Alice"}is valid JavaScript but invalid JSON. It must be{"name": "Alice"}. - No trailing commas. The last item in an object or array must not have a comma after it.
[1, 2, 3,]is invalid JSON. - No comments. JSON does not have a comment syntax.
// this is a commentinside a JSON file breaks parsing. - Strings use double quotes only. Single quotes (
'value') are not allowed anywhere in JSON. - Special characters in strings must be escaped. A literal double quote inside a string must be written as
\"; a backslash as\\; a newline as\n. - Numbers cannot have leading zeros.
007is invalid; write7.
How JSON is used in practice
JSON shows up in a wide range of situations in software development:
- REST APIs. The overwhelming majority of web APIs send and receive data as JSON. Your client sends a JSON request body; the server replies with a JSON response. Frameworks in every language parse and serialize JSON automatically.
- Configuration files. Tools like VS Code, ESLint, npm, and many others use
.jsonfiles for their configuration. You have almost certainly edited apackage.jsonortsconfig.json. - Database storage. PostgreSQL has a native
jsonbcolumn type; MongoDB stores documents in a JSON-like binary format (BSON); many other databases support JSON columns for semi-structured data. - Browser storage.
localStorageandsessionStoragestore strings, so developers serialize objects to JSON withJSON.stringify()before writing and deserialize withJSON.parse()on read. - Data exchange files. Data exports from analytics platforms, CRMs, and internal tools are often delivered as JSON files, especially when the structure is irregular enough that CSV would be awkward.
JSON vs XML: why JSON won
Before JSON became dominant, XML was the standard for web data exchange. Both formats represent structured data as text, but they differ in verbosity and ergonomics:
| Feature | JSON | XML |
|---|---|---|
| Syntax | Key-value pairs, arrays | Opening and closing tags |
| Verbosity | Compact | More verbose (repeated tag names) |
| Comments | Not supported | Supported (<!-- -->) |
| Native JS parsing | JSON.parse() built in | Requires DOMParser or a library |
| Schema validation | JSON Schema (external) | XSD, DTD (built into ecosystem) |
| Best for | Web APIs, config, app data | Documents, SOAP, enterprise systems |
JSON is smaller for equivalent data, maps directly onto the objects and arrays native to most programming languages, and is trivial to parse in JavaScript without any library. Those three advantages made it the default choice for web APIs as REST displaced SOAP, and it has stayed there.
Reading and writing JSON in code
Every major language has built-in or standard-library support for JSON. The pattern is always the same: parse a JSON string into a native data structure, and serialize a native data structure back to a JSON string.
In JavaScript:
// Parse a JSON string into a JS object
const user = JSON.parse('{"name":"Alice","age":30}');
console.log(user.name); // "Alice"
// Serialize a JS object to a JSON string
const json = JSON.stringify({ name: "Bob", active: true }, null, 2);
// The third argument (2) sets the indentation for pretty-printing
In Python:
import json
# Parse JSON string to dict
user = json.loads('{"name": "Alice", "age": 30}')
print(user["name"]) # Alice
# Serialize dict to JSON string
output = json.dumps({"name": "Bob", "active": True}, indent=2)
The pattern is identical in Go (encoding/json), Rust (serde_json), Java (Jackson or Gson), and PHP (json_encode / json_decode). The names differ; the concept does not.
Common mistakes and how to fix them
When a JSON document fails to parse, the error message usually points to a line and column. The most frequent culprits:
- Trailing comma — remove the comma after the last item in an object or array.
- Single-quoted strings — replace every
'...'with"...". - Unquoted keys — wrap every key in double quotes:
name:becomes"name":. - Comments — delete any
//or/* */comments. If you need comments in a config file, look for a format that supports them (like JSON5 or TOML) or move comments to a separate README. - Unescaped special characters — a literal tab or newline inside a string must be written as
\tor\n.
If you are staring at a blob of minified JSON (everything on one line, no spaces), it is nearly impossible to spot these issues by eye. Paste it into a formatter and the structure immediately becomes visible — and the validator will highlight exactly where the error is.
Format, validate, or minify JSON instantly
JSONForge is Figro's free, private JSON tool — format messy JSON into something readable, validate a document for syntax errors, minify for production, or explore your data with an interactive tree view. No signup, nothing uploaded to any server.
Open the free JSON formatter →Figro's guides are educational and independent. They are not professional or legal advice. Some pages include affiliate links; if you purchase through them we may earn a commission at no extra cost to you.