What is JSON?
JSON (JavaScript Object Notation, RFC 8259) is a plain-text format for structured data built from objects, arrays and a handful of value types. It is easy for humans to read and every mainstream language can parse it, so it is the default format for web APIs and for config files such as package.json.
| Type | Example |
|---|---|
| String | "Alice" (double quotes only) |
| Number | 30, 3.14, -1e10 |
| Boolean | true, false |
| Null | null |
| Array | [1, 2, 3] |
| Object | {"a": 1} |
Formatting {"name":"Alice","age":30,"tags":["admin","dev"]} with 2-space indentation gives:
{
"name": "Alice",
"age": 30,
"tags": [
"admin",
"dev"
]
}
Common JSON syntax errors
| Problem | Invalid | Valid |
|---|---|---|
| Trailing comma | {"a": 1,} | {"a": 1} |
| Single quotes | {'a': 1} | {"a": 1} |
| Unquoted key | {a: 1} | {"a": 1} |
| Comments | // note | Not allowed in JSON |
| undefined / NaN | {"a": undefined} | {"a": null} |
Trailing commas and comments are accepted by JavaScript object literals and by “JSON with comments” formats such as JSONC or JSON5, but not by a standard JSON parser.
Format JSON in code and on the command line
| Tool | Pretty-print | Minify |
|---|---|---|
| jq | jq . data.json | jq -c . data.json |
| Python | python3 -m json.tool data.json | python3 -m json.tool --compact data.json |
| JavaScript | JSON.stringify(obj, null, 2) | JSON.stringify(obj) |
| PHP | json_encode($v, JSON_PRETTY_PRINT) | json_encode($v) |
Escape sequences in JSON strings
Inside a JSON string, a double quote, a backslash and control characters (U+0000 to U+001F, such as a raw line break) must be escaped. These are the only escapes JSON allows:
| Escape | Meaning |
|---|---|
\" | Double quote |
\\ | Backslash |
\/ | Forward slash (optional) |
\n / \r / \t | Line feed / carriage return / tab |
\b / \f | Backspace / form feed |
\uXXXX | Any character by its UTF-16 code unit, e.g. \u00e9 = é |
Other escapes such as \x41 or \' are valid in JavaScript but make the JSON invalid. A raw line break inside a string is another common cause of “Unexpected token” errors when JSON is built by hand.
JSON vs. a JavaScript object
| JSON | JavaScript object literal | |
|---|---|---|
| Keys | Always double-quoted strings | Quotes optional |
| Strings | Double quotes only | Single, double or backticks |
| Trailing commas, comments | Not allowed | Allowed |
| Values | string, number, boolean, null, array, object | Also functions, undefined, Date, NaN… |
JSON.stringify drops properties whose value is undefined or a function, turns NaN and Infinity into null, and converts a Date to an ISO 8601 string.
Pretty-print JSON in your editor or browser
- VS Code: open the file and run “Format Document” (Shift+Alt+F on Windows/Linux, Shift+Option+F on macOS).
- Firefox shows JSON responses in a built-in viewer with a “Pretty Print” option; Chrome shows a “Pretty-print” checkbox above raw JSON responses.
- Browser DevTools: in the Network panel, the Preview tab of a request shows the response as a collapsible tree.
Frequently asked questions
Why do the last digits of a large number change?
JavaScript numbers are 64-bit floating point and represent integers exactly only up to 253 − 1 (9007199254740991). A larger integer is rounded when parsed — 9007199254740993 becomes 9007199254740992. If you need exact large IDs, send them as strings.
What is “Sort keys” for?
It orders object keys alphabetically at every level of nesting. Sorting two documents before comparing them makes a diff show only real differences, not differences in key order.
Can it decode \uXXXX escapes?
Yes. Formatting parses the JSON, so escapes such as é are shown as the actual character (é). To convert escapes in plain text, use the Unicode escape tool.
Is it safe to paste an API response?
The JSON is parsed and formatted in your browser and is not sent to any server. Still, be careful where you paste tokens or personal data in general.