What a JSON formatter does
JSON (JavaScript Object Notation) is the most common format for sending data between programs. APIs usually return it as one long line with no spaces, because whitespace adds bytes and the computer does not need it. People do need it. A JSON formatter adds line breaks and indentation so you can see the structure: which values belong to which object, and where each array starts and ends.
This tool does three jobs:
- Format turns compact JSON into readable, indented JSON.
- Minify removes every space and line break that is not inside a string.
- Validate checks the JSON against the standard and reports the first problem it finds.
How to use it
- Paste your JSON into the left box. It is formatted as soon as you paste. You can also press Open file to load a
.jsonfile, or Sample to try an example. - Pick an Indent: 2 spaces, 4 spaces or Tabs.
- Press Format, Minify or Validate. Ctrl+Enter runs the last action from the keyboard.
- Turn on Sort keys A to Z if you want alphabetical keys.
- Press Copy, or Download to save the result as a
.jsonfile.
After the first run, the result updates as you type, so you can fix a problem and watch it clear.
Before and after
This is a typical API response with no whitespace:
{"id":42,"name":"Ada Lovelace","active":true,"roles":["admin","editor"],"address":{"city":"London","zip":"NW1"},"score":98.50,"manager":null}
After Format with 2 spaces:
{
"id": 42,
"name": "Ada Lovelace",
"active": true,
"roles": [
"admin",
"editor"
],
"address": {
"city": "London",
"zip": "NW1"
},
"score": 98.50,
"manager": null
}
Note that 98.50 stays 98.50. The formatter reads your text with its own parser and copies numbers character for character. A large ID such as 12345678901234567890 also stays exact, while a tool built on JSON.parse would print 12345678901234567000.
With Sort keys A to Z and 4 spaces, {"name":"Ada","age":36,"address":{"zip":"NW1","city":"London"}} becomes:
{
"address": {
"city": "London",
"zip": "NW1"
},
"age": 36,
"name": "Ada"
}
Validate gives a short summary for valid input. For the first example it reports: “Valid JSON. The root is an object with 12 values in total, nested 3 deep.”
Error messages you might see
When the JSON is broken, the message below the boxes gives the line and column, and Show in input jumps the cursor to that spot. These are real messages from the tool:
| Mistake | Message |
|---|---|
| Comma after the last item | Line 4, column 1: Trailing comma before “}” is not allowed |
| Single quotes | Line 1, column 2: Expected a property name in double quotes but found a single quote (JSON strings need double quotes) |
| Missing comma between items | Line 3, column 3: Expected “,” or “}” after a value but found “”” |
| Key without quotes | Line 1, column 2: Expected a property name in double quotes but found “n” |
A // comment |
Line 2, column 3: Comments are not allowed in JSON |
These rules come from the JSON standard. JavaScript object literals allow single quotes, unquoted keys and trailing commas, which is why code copied from a .js file often fails here. If your data really is JavaScript, the JavaScript Formatter is the better fit.
Tips and limits
- Strict JSON only.
undefined,NaN, comments and trailing commas are reported as errors, because other programs will reject them too. - Duplicate keys are kept as written. The formatter does not merge or drop them.
- Minify keeps the content of strings untouched, including spaces inside them.
- To check a whole file, use Open file. Everything stays on your device.
Other ways to format JSON
VS Code. Open a .json file and run Format Document from the command palette. VS Code uses its built-in JSON formatter.
Command line with jq. jq . data.json prints formatted JSON, and jq -c . data.json prints it compact. jq also lets you filter and reshape data.
Command line with Python. python -m json.tool data.json formats a file with 4 spaces. It is part of the standard library, so it works wherever Python is installed.
In JavaScript. JSON.stringify(JSON.parse(text), null, 2) formats a string, but it goes through JavaScript numbers, so long IDs can lose digits.
Related tools
To turn JSON into a config file, use the JSON to YAML Converter. To embed JSON inside another JSON string, use JSON Stringify Text. To build JSON from a spreadsheet export, try CSV to JSON.