Quick answer: The fastest way to format JSON is to paste it into an online formatter, which indents and validates it instantly with nothing to install. On the command line, run
jq . file.jsonorpython -m json.tool file.json. In code, callJSON.stringify(data, null, 2), and in VS Code, JetBrains IDEs, or Sublime Text use the built-in format command so files are indented consistently every time you save.
You copy an API response. It is one line. Four hundred characters of nested brackets, quotes, and commas. You need to find one field buried inside it. Good luck.
Formatting JSON is not about making things pretty. It is about making them readable. Here are five methods that actually work, from the fastest option to the most programmable, followed by advice on indentation, other editors, and the mistakes that cost people time.
Open a valid sample in the JSON Viewer to explore objects and arrays as collapsible nodes alongside the formatted code.
Method 1: Online JSON Formatter (Fastest)
The fastest way to format JSON is to paste it into an online tool. No installation, no terminal, no configuration. You paste, you read, you move on.
Our online JSON formatter validates your JSON as you type, highlights syntax errors in real time, and lets you toggle between 2-space and 4-space indentation. You can copy the formatted result or download it as a file.
Online formatters are ideal when you are debugging an API response on a colleague's laptop, inspecting a webhook payload on your phone during an outage, or working on a restricted machine where you cannot install tools. If there is a browser, it works. For multi-megabyte payloads, see validating large JSON in the browser before you paste.
Method 2: Command Line with jq
jq is the Swiss Army knife of command-line JSON processing. Install it once and you will use it every day:
# macOS
brew install jq
# Ubuntu/Debian
sudo apt install jq
# Windows (with scoop)
scoop install jq
Basic formatting is one command:
echo '{"name":"Dana","age":30}' | jq .
But jq does far more than pretty-printing. You can extract fields, filter arrays, and build pipelines:
# Extract a single field
echo '{"name":"Dana","age":30}' | jq '.name'
# Format a file with 2-space indent
jq --indent 2 . data.json
# Get the first item from an array
curl -s https://api.example.com/users | jq '.[0]'
jq pays for itself within a week. If you work with JSON on the command line regularly, it is the single most valuable tool you can install. Our jq cheat sheet collects the filters you will reach for most.
Method 3: Python's json.tool
If you have Python installed (and you probably do), you already have a JSON formatter. No extra packages needed:
echo '{"name":"Dana","age":30}' | python -m json.tool
Output:
{
"name": "Dana",
"age": 30
}
It uses 4-space indentation by default. You can also pass a file path directly:
python -m json.tool data.json
Recent Python versions accept --indent 2 and --sort-keys flags, and for anything more involved json.dumps(data, indent=2) inside a script gives you full control. The Python json module guide covers the full API. For a quick one-off format, it is hard to beat something that ships with the language.
Method 4: VS Code
Visual Studio Code formats JSON files natively. Open any .json file and use the keyboard shortcut:
- Windows/Linux:
Shift + Alt + F - macOS:
Shift + Option + F
Even better, you can enable format-on-save so your JSON files are always clean:
// settings.json
{
"[json]": {
"editor.tabSize": 2,
"editor.formatOnSave": true
}
}
With this configuration, every time you save a JSON file, VS Code automatically formats it with 2-space indentation. This is especially useful for config files checked into version control - no more messy diffs caused by inconsistent formatting. VS Code also underlines syntax errors as you type; validating JSON in VS Code shows how to attach a schema for stricter checks.
Method 5: JavaScript's JSON.stringify()
JSON.stringify() accepts three arguments. The third one controls indentation:
const data = { name: "Dana", age: 30, roles: ["admin", "editor"] };
// Minified (no formatting)
JSON.stringify(data);
// '{"name":"Dana","age":30,"roles":["admin","editor"]}'
// Pretty-printed with 2 spaces
JSON.stringify(data, null, 2);
// Pretty-printed with 4 spaces
JSON.stringify(data, null, 4);
The second argument is a replacer function or array - passing null means "include everything." The third argument is the indent: a number for spaces or a string for custom indentation.
A practical use case - writing a formatted JSON file in Node.js:
const fs = require("fs");
const config = {
port: 3000,
database: { host: "localhost", name: "myapp" }
};
fs.writeFileSync(
"config.json",
JSON.stringify(config, null, 2) + "\n"
);
Adding \n at the end ensures the file ends with a newline, which keeps Git happy and follows POSIX conventions. For the replacer argument and other tricks, see working with JSON in JavaScript.
Formatting in Other Editors
VS Code is not the only editor with a built-in formatter.
JetBrains IDEs (IntelliJ IDEA, WebStorm, PyCharm, PhpStorm). Open the file and run Code > Reformat Code - Ctrl + Alt + L on Windows and Linux, Cmd + Option + L on macOS. The indent size comes from Settings > Editor > Code Style > JSON, and you can enable Reformat code under Actions on Save so it happens automatically. JetBrains also pulls schemas from SchemaStore for common files like package.json and tsconfig.json, so it will flag unknown keys as well as broken syntax.
Sublime Text. There is no JSON formatter out of the box. Install the Pretty JSON package through Package Control, then press Ctrl + Alt + J (Windows/Linux) or Cmd + Ctrl + J (macOS). The package also offers a minify command and can validate the document.
Vim and Neovim. Filter the buffer through an external tool: :%!jq . or :%!python -m json.tool. Both replace the file contents in place, so make sure the JSON is valid first or you will end up with an error message where your data used to be.
Browser DevTools. If the JSON is coming from a network request, you may not need to copy it at all - the Network panel's Preview tab renders it as a collapsible tree. See format JSON in Chrome DevTools.
Choosing Your Indent Size
The two common choices are 2 spaces and 4 spaces. The JavaScript ecosystem has largely settled on 2 spaces - package.json, tsconfig.json, and most linter configs use 2-space indentation. Python tends toward 4 spaces, matching PEP 8 conventions, and python -m json.tool outputs 4 by default.
Tabs are legal JSON whitespace too, and JSON.stringify(data, null, "\t") will produce them. In practice they are rare in JSON files because most formatters, APIs, and code review tools assume spaces, and a tab-indented file dropped into a 2-space repository produces a noisy diff for no benefit. Use tabs only if the rest of the project already does.
For deeply nested JSON, 2 spaces keep lines shorter and more scannable. For flat structures, it barely matters. Pick whichever your team uses, encode it in an .editorconfig or formatter config, and stay consistent. The worst indent style is an inconsistent one.
Common Mistakes When Formatting JSON
- Formatting invalid JSON. Every formatter needs to parse the input first, so a trailing comma or a single-quoted string stops it cold. Fix the syntax before you format; fixing trailing commas and the Unexpected token error cover the usual culprits.
- Round-tripping big integers through JavaScript.
JSON.parse()converts numbers to 64-bit floats, so an ID like9007199254740993changes value beforeJSON.stringify()writes it back out. Usejqor a text-level formatter for payloads with large IDs, or read JSON numbers, precision, and BigInt. - Formatting JSON Lines as one document. A
.jsonlor NDJSON file holds one JSON value per line. Pretty-printing the whole file as a single document fails, and reformatting individual lines breaks the one-record-per-line contract. Format a single line at a time instead. - Mixing indent styles in one repository. Two developers, two editor settings, and every commit reformats the file. Agree on one width and enforce it with format-on-save.
- Shipping pretty-printed JSON to production. Indentation is for humans. A minified payload is typically 15-30% smaller and parses just as fast, so format for reading and minify for the wire.
- Pasting a formatted document into a string literal. The line breaks and double quotes then need escaping, and it is easy to get wrong. Keep JSON in its own file or build it with
JSON.stringify()instead.
When to Minify Instead
Formatting is about readability. But sometimes you need the opposite - removing all whitespace to reduce file size for production payloads. A typical JSON response shrinks by 15-30% when minified, and for APIs serving thousands of clients, that adds up fast.
For a complete guide on stripping whitespace from JSON, see how to minify JSON. And if you are brand new to the format itself, start with our introduction to what JSON is and how it works.