How to Format and Pretty-Print JSON (Browser, Code and Command Line)
Basics 7 min readUpdated October 12, 2026
Minified JSON is great for machines and miserable for people. Formatting (also called pretty-printing or beautifying) adds line breaks and indentation so you can see the structure at a glance. This guide shows every common way to do it, explains which indentation to choose, and points out one subtle trap that can silently corrupt large numbers.
What formatting changes, and what it does not
Whitespace between JSON tokens has no meaning. These two documents describe exactly the same data:
{"id":42,"name":"Asha","tags":["admin","beta"],"address":{"city":"Pune","zip":"411001"}}{
"id": 42,
"name": "Asha",
"tags": [
"admin",
"beta"
],
"address": {
"city": "Pune",
"zip": "411001"
}
}A formatter only touches whitespace. The keys, values and their order should come out exactly as they went in. If a tool reorders keys or changes values while it formats, treat that as a bug, or as a different operation such as sorting.
Format JSON in a browser tool
For a quick one-off job, paste the text into an online editor and run its pretty-print command. A good editor also validates as it formats, so a missing comma or quote is reported instead of silently producing nothing. With TrueJson you paste into the editor and use Pretty Print on the Format ribbon; the Minify command does the reverse.
Browser tools are convenient, but think before pasting production secrets, access tokens or personal data into any website. Editors that process your text locally in the browser are safer than ones that send it to a server, and a quick look at the network tab of your browser's developer tools will tell you which kind you are using.
Format JSON in code
Every mainstream language can parse JSON and write it back with indentation.
JavaScript and TypeScript
const text = '{"id":42,"name":"Asha","tags":["admin","beta"]}';
const data = JSON.parse(text);
// The third argument is the indentation: a number of spaces, or a string such as "\t"
console.log(JSON.stringify(data, null, 2));Python
import json
text = '{"id": 42, "name": "Asha", "tags": ["admin", "beta"]}'
data = json.loads(text)
print(json.dumps(data, indent=2, ensure_ascii=False))Java (Jackson)
ObjectMapper mapper = new ObjectMapper();
JsonNode tree = mapper.readTree(text);
String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tree);C# (System.Text.Json)
using System.Text.Json;
var doc = JsonDocument.Parse(text);
var options = new JsonSerializerOptions { WriteIndented = true };
string pretty = JsonSerializer.Serialize(doc.RootElement, options);Format JSON on the command line
The command line is the fastest option for files and for the output of other commands, such as an API call.
# jq: the most popular tool, "." means "the whole document"
jq . data.json
# Pipe an API response straight into it
curl -s https://api.example.com/users/42 | jq .
# Python's standard library works anywhere Python is installed
python -m json.tool data.json
# Write the formatted result to a new file
jq . data.json > data.pretty.jsonjq -S . additionally sorts object keys alphabetically, which is useful before comparing two files. See the guide on sorting JSON for why that matters.
Choosing an indentation style
| Style | Good for | Watch out for |
|---|---|---|
| 2 spaces | Most web projects; compact but readable. The default in many JavaScript tools. | Very deep nesting can still be hard to follow. |
| 4 spaces | Teams that follow Python or C# conventions. | Wide lines in deeply nested documents. |
| Tabs | Readers who set their own tab width. | Tabs render differently across tools and diffs. |
The most important rule is consistency. If configuration files are committed to version control, agree on one style and let a formatter apply it automatically, so that diffs show real changes and not whitespace noise.
The big-number trap
Many formatters work by parsing the text into objects and writing it back out. In JavaScript that means numbers become 64-bit floating point values, which can only represent integers exactly up to 9,007,199,254,740,991 (that is Number.MAX_SAFE_INTEGER). Larger integers, such as the 64-bit identifiers used by many databases and social networks, are silently rounded:
const text = '{"orderId": 12345678901234567890}';
console.log(JSON.stringify(JSON.parse(text)));
// {"orderId":12345678901234567000} <- the last digits changedHow to stay safe
If your data contains integers longer than 15 or 16 digits, send and store them as strings, or use a formatter that works on the text instead of parsing numbers. Python's json module keeps arbitrary-size integers exact, and jq 1.7 and later preserves the original text of large number literals. Always compare the result with the original when the identifiers matter.
Formatting invalid JSON
Parse-based formatters cannot format text that is not valid JSON, because they cannot parse it. They will report an error, usually with a line and column. Fix the syntax first. The most common causes are trailing commas, single quotes, unquoted keys and comments, all covered in our guide to common JSON errors.
Frequently asked questions
Does formatting JSON change its meaning?
No. Whitespace outside of strings is ignored by every JSON parser, so formatted and minified versions are equivalent. Only whitespace inside a string value matters, and formatters never touch that.
Is pretty-printed JSON slower for APIs?
It is larger, because of the extra whitespace, so send minified JSON over the network and format it only for humans. With compression such as gzip the size difference shrinks a lot, but there is still no reason to pay for whitespace nobody reads.
How do I format a very large JSON file?
Browser editors can struggle with files of many megabytes. Use a command-line tool such as jq, which is built for large inputs, or work on a smaller sample of the file.
Why did my formatter change the order of keys?
It should not. JSON objects are technically unordered, but a pure formatter keeps the original order. If keys moved, the tool is also sorting them, or it is rebuilding the object from a data structure that does not keep insertion order.