JSON (JavaScript Object Notation) is the most common way to exchange data on the web. APIs return it, config files use it, logs are full of it. Despite the “JavaScript” in the name, it’s language-independent — Python, Go, Java and virtually everything else can read and write it. Here’s what you need to know in five minutes.
The building blocks
A JSON document is built from just a few types:
- Object — an unordered set of key/value pairs wrapped in curly braces:
{ "name": "Ada", "age": 36 } - Array — an ordered list wrapped in square brackets:
[1, 2, 3] - String — text in double quotes:
"hello" - Number —
42,3.14,-7(no quotes) - Boolean —
trueorfalse - null — an explicit empty value
Objects and arrays can nest inside each other to any depth, which is how JSON represents complex data.
A quick example
{
"user": "ada",
"active": true,
"roles": ["admin", "editor"],
"profile": { "age": 36, "city": "London" }
}
That’s a single object with a string, a boolean, an array of strings, and a nested object.
The rules that trip people up
Most “invalid JSON” errors come from a small set of mistakes:
- Trailing commas.
[1, 2, 3,]is valid in JavaScript but not in JSON. Remove the comma after the last item. - Single quotes. JSON requires double quotes for both keys and string values.
{'name': 'Ada'}is invalid; it must be{"name": "Ada"}. - Unquoted keys.
{name: "Ada"}is a JavaScript object literal, not JSON. Keys must be quoted strings. - Comments. JSON has no comment syntax.
// like thiswill cause a parse error. - Wrong value types. Numbers and booleans must not be quoted unless you actually want them as strings.
How to fix a broken JSON quickly
When your code throws a parse error, the fastest way to find the problem is to run the document through a validator. Paste it into the JSON formatter: if the syntax is wrong, it points to the approximate location and tells you why. Once it’s valid, click Beautify to see the structure clearly, or Minify to shrink it before sending it over the network.
When (not) to use JSON
JSON is great for configuration and API payloads. It’s less ideal for very large datasets (streaming formats or binary formats are more efficient) or for documents that need comments (YAML or TOML are friendlier there). But for the vast majority of web work, JSON is the lingua franca — and now you can read it fluently.
Ready to tidy up some JSON? Try the free JSON formatter — it runs entirely in your browser, so your data never leaves your device.