What Is JSON and How to Format It?
JSON is the language the internet uses to move data around. Nearly every API you have ever called returned JSON; nearly every modern config file is JSON or something inspired by it. Its great virtue is that it is boring, small and strict — but that strictness is exactly what produces the maddening Unexpected token errors that eat an afternoon. This guide covers the rules, the traps, and how to read an error message properly.
What JSON is
JSON stands for JavaScript Object Notation. It is a text format for structured data — a way of writing down objects, lists, numbers and strings so that any program, in any language, can read them back identically.
Despite the name, JSON is not JavaScript and is not tied to it. It was derivedfrom JavaScript's syntax, but every serious language reads and writes it. That universality is the whole point.
It replaced XML for most purposes because it is dramatically less verbose. The same data in XML is typically two to three times larger and considerably harder to read.
The six data types — and that is all
JSON supports exactly six types. There is no seventh, and this is a common source of surprise:
- String —
"hello". Must use double quotes. - Number —
42,3.14,-7. No distinction between integers and floats. - Boolean —
trueorfalse. Lowercase only. - Null —
null. Lowercase. - Object —
{ "key": "value" }. An unordered set of key/value pairs. - Array —
[1, 2, 3]. An ordered list.
Notice what is missing: there is no date type. Dates are conventionally written as strings, usually in ISO 8601 format ("2026-07-13T10:00:00Z"), and every program must agree to interpret them. There are also no comments, and no functions.
The rules that break people
JSON is strict by design — an unambiguous format is one every parser agrees on. But the strictness catches everyone, because JSON looks like JavaScript while forbidding several things JavaScript allows.
- Keys must be double-quoted strings.
{ name: "Alice" }is valid JavaScript and invalid JSON. It must be{ "name": "Alice" }. - Only double quotes. Single quotes are never allowed — not for keys, not for values.
{ 'name': 'Alice' }is invalid. - No trailing commas.
{ "a": 1, "b": 2, }is invalid. This is probably the single most common JSON error in existence — and it is legal in JavaScript, which is exactly why it catches people. - No comments. Neither
//nor/* */. There is no way to annotate JSON, which is a real frustration in config files, and why formats like JSON5 and YAML exist. - No undefined, no
NaN, noInfinity. - Strings must escape special characters. A literal double quote inside a string is
\", and a backslash is\\. A raw newline inside a string is invalid — it must be\n.
How to read a JSON error message
Errors usually look like Unexpected token } at position 47. The vital thing to know is that the position reported is where the parser gave up, not where you made the mistake.
A missing comma on line 10 is only detected when the parser reaches the next token that no longer makes sense — which might be line 11 or 12. So always look at the lines just before the reported position.
Working through the most common causes, in order of likelihood:
- A trailing comma before a
}or]. - A missing comma between two items.
- Single quotes instead of double.
- An unquoted key.
- An unescaped quote inside a string.
- A mismatched bracket or brace.
A formatter that validates as it goes will point you at the real problem in seconds — far faster than staring at it.
Prettify vs. minify
The same JSON data can be written two ways, and both are valid:
Prettified — indented, one field per line. Vastly easier for a human to read and debug. Use this while developing, and whenever you are inspecting an API response.
Minified — all whitespace stripped, everything on one line. Smaller, so it transmits faster. Use this in production for anything sent over a network.
Whitespace has no meaning in JSON, so converting between the two changes nothing about the data. Machines do not care; humans care enormously. A formatter simply lets you switch.
A worked example
Here is well-formed JSON showing most of the types at once:
{ "id": 1, "name": "Alice", "active": true, "tags": ["admin", "editor"], "manager": null, "address": { "city": "Dhaka" } }
Objects nest inside objects, arrays hold any type, and the structure can go as deep as you need. That composability is why JSON handles almost any shape of data.
Common mistakes to avoid
- Trailing commas. Legal in JavaScript, fatal in JSON.
- Single quotes. Never valid.
- Trying to add comments. JSON has none. Use a
"_comment"field if you must. - Treating a JSON string as an object. A response is text until you parse it.
- Assuming key order is preserved. JSON objects are formally unordered — never rely on the order of keys.
- Very large numbers. Integers beyond about 2⁵³ lose precision in JavaScript. Send them as strings.
Frequently asked questions
Is JSON the same as a JavaScript object? No. JSON is text — a string. A JavaScript object is a live in-memory structure. You parse JSON into an object.
Can JSON have comments? No. This is a deliberate design decision, and a frequently cursed one.
How do I store a date in JSON? As an ISO 8601 string, such as "2026-07-13T10:00:00Z".
Is minified JSON faster? It transmits faster because it is smaller. Parsing speed is essentially identical.
Format your JSON now
Use our JSON Formatter & Validator to prettify, minify and validate JSON in your browser — errors are pinpointed, and nothing is uploaded, so it is safe with real API responses. Working from spreadsheet data? Try the CSV to JSON Converter.