Guide · 4 min read
What is JSON, and how do you format it?
A plain-English introduction to JSON: its six data types, the syntax rules that trip people up, and when to beautify or minify it.
JSON (JavaScript Object Notation) is a text format for structured data. Web APIs use it to send responses, applications use it for configuration files, and databases use it to export records. It is easy for people to read and easy for programs to parse — as long as the syntax is exactly right.
The building blocks
JSON has six types of value:
- Object — key–value pairs in curly braces:
{"name": "Ann"} - Array — an ordered list in square brackets:
[1, 2, 3] - String — text in double quotes:
"hello" - Number —
42,-3.5,1e6 - Boolean —
trueorfalse - Null —
null
Objects and arrays can be nested inside each other to any depth:
{
"order": 1042,
"paid": true,
"items": [
{ "sku": "MUG-01", "qty": 2 },
{ "sku": "TEE-M", "qty": 1 }
]
}The rules that cause most errors
JSON looks like JavaScript but is much stricter. These are the usual culprits:
- Trailing commas —
[1, 2,]is invalid. - Single quotes — strings and keys need double quotes.
- Unquoted keys —
{name: "Ann"}is invalid; it must be{"name": "Ann"}. - Comments —
//and/* */aren’t allowed. - Special numbers —
NaN,Infinityand leading zeros such as007aren’t valid.
Beautify or minify?
Beautifying (pretty-printing) adds line breaks and indentation so people can read and review the structure. Use it when debugging an API response or editing a config file.
Minifying removes all unnecessary whitespace. The data is identical but smaller, which is useful when sending JSON over a network or storing lots of it.
Format and validate in one step
Paste your data into the JSON formatter. It checks the syntax, tells you the line and column of the first error with a plain-English explanation, and lets you beautify, minify or sort keys. Your JSON is processed in your browser, which matters when it contains customer data or internal identifiers.