How to Format and Validate JSON: Prettify, Minify, and Fix Errors Online
How to format JSON in JavaScript, Python, VS Code, and the command line — plus how to fix invalid JSON that refuses to format. Includes JSON.stringify options, jq, and Python json.tool.
Formatting JSON means converting minified or unreadable JSON text into an indented, human-readable structure — or the reverse: compressing it for production use. This guide covers how to format JSON online, in code, in VS Code, and in the command line, plus how to find and fix the errors that prevent formatting from working.
What is the difference between formatted and minified JSON?
Formatted (pretty-printed) JSON adds indentation and line breaks to show the structure clearly. Minified JSON removes all whitespace to reduce file size. Both represent identical data — only the whitespace differs:
// Formatted (pretty-printed) — for reading and debugging
{
"name": "Alice",
"age": 31,
"tags": ["developer", "python"]
}
// Minified — for APIs and production use
{"name":"Alice","age":31,"tags":["developer","python"]}Use formatted JSON when reviewing, debugging, or committing to version control. Use minified JSON for API responses and network transmission where byte size matters.
How do you format JSON online instantly?
The fastest method: paste your JSON into the JSON Formatter & Validator. It formats the JSON with 2-space indentation, highlights any syntax errors with the exact line number, and lets you copy the result with one click. All processing happens in your browser — no data is sent to a server.
This is particularly useful when you receive a minified API response and need to read it, or when you paste JSON from a PDF or email and need to check whether it is valid before using it in code.
How do you format JSON in JavaScript?
JavaScript's built-in JSON.stringify() handles both formatting and minification:
const data = { name: "Alice", age: 31, tags: ["developer", "python"] };
// Format with 2-space indentation (pretty-print)
const formatted = JSON.stringify(data, null, 2);
console.log(formatted);
// {
// "name": "Alice",
// "age": 31,
// "tags": [
// "developer",
// "python"
// ]
// }
// Minify (remove all whitespace)
const minified = JSON.stringify(data);
// {"name":"Alice","age":31,"tags":["developer","python"]}
// Custom indentation — 4 spaces
const wide = JSON.stringify(data, null, 4);
// Indentation with tabs
const tabbed = JSON.stringify(data, null, ' ');The three arguments to JSON.stringify(): the value to serialise, a replacer function or null, and the indentation level (number of spaces or a string like '\t').
How do you format JSON in Python?
import json
data = {"name": "Alice", "age": 31, "tags": ["developer", "python"]}
# Pretty-print to console
print(json.dumps(data, indent=2))
# Write formatted JSON to file
with open("output.json", "w") as f:
json.dump(data, f, indent=2)
# Format a JSON string (string in → string out)
raw = '{"name":"Alice","age":31}'
parsed = json.loads(raw)
formatted = json.dumps(parsed, indent=2)
# Sort keys alphabetically (useful for diffs)
sorted_json = json.dumps(data, indent=2, sort_keys=True)
# Minify a file
with open("input.json") as f:
minified = json.dumps(json.load(f), separators=(",", ":"))
How do you format JSON in VS Code?
VS Code has built-in JSON formatting. Three methods:
- Format document: Open a
.jsonfile → press Shift+Alt+F (Windows/Linux) orShift+Option+F (Mac). VS Code formats the entire file with the configured indentation. - Format on save: Add
"editor.formatOnSave": trueto VS Code settings. Every time you save a JSON file, it formats automatically. - Paste and format: Copy minified JSON, open a new file with
.jsonextension, paste, then format. VS Code also validates JSON syntax in real time — red underlines mark errors.
How do you format JSON from the command line?
# Using jq (most powerful — install with brew install jq or apt install jq)
cat data.json | jq .
echo '{"name":"Alice","age":31}' | jq .
# Minify with jq
cat data.json | jq -c .
# Using Python (no install needed)
cat data.json | python3 -m json.tool
echo '{"name":"Alice"}' | python3 -m json.tool
# Using Node.js
node -e "const d=require('./data.json'); console.log(JSON.stringify(d,null,2))"
# Format and write to file
cat minified.json | python3 -m json.tool > formatted.jsonWhat does "invalid JSON" mean and how do you fix it?
If your JSON formatter shows an error before it can format the output, the input is not valid JSON. The formatter cannot add indentation to a string that fails to parse. The five most common causes:
- Trailing comma after the last property:
{"a": 1,}→ remove the comma - Single quotes instead of double:
{'key': 'value'}→ use double quotes - Unquoted keys:
{key: "value"}→ add double quotes around the key - Missing comma between items:
{"a": 1 "b": 2}→ add a comma after1 - Comments:
// this breaks JSON→ remove the comment
For a complete guide to every JSON error and how to fix it, see how to fix JSON parse errors and unexpected token issues. For a conceptual understanding of why JSON has these strict rules, see the complete JSON guide.
Key takeaways
- Pretty-printed and minified JSON represent the same data — only whitespace differs.
- In JavaScript:
JSON.stringify(data, null, 2)for 2-space formatted output. - In Python:
json.dumps(data, indent=2)for formatted output. - In VS Code: Shift+Alt+F formats any open JSON file.
- From the command line:
cat file.json | jq .orpython3 -m json.tool. - If the formatter shows an error, fix the JSON syntax first — formatting cannot run on invalid JSON.
Frequently asked questions
Why does my JSON formatter say the input is invalid?
The most common causes: trailing comma after the last item, single quotes instead of double quotes, an unquoted key, a missing comma between properties, or a comment (JSON does not support comments). The error message will show a position number — count to that position or paste into the JSON Formatter & Validator to see the highlighted error line.
Is there a way to auto-fix invalid JSON?
For common mistakes, the json-repair npm package and Python'sjson-repair library automatically fix trailing commas, single quotes, and other common errors. For one-off fixes, the JSON Formatter highlights the error location so you can correct it manually in under a minute.
How do I format a large JSON file without loading it into memory?
Use jq from the command line — it processes files in a streaming fashion: jq . large-file.json > formatted.json. For very large files (100MB+), jq with the --stream flag processes without loading the full file into memory.
What indentation style should I use for JSON files — 2 spaces or 4?
2-space indentation is the most common convention for JSON files — used by npm's package.json, most API documentation, and Prettier's JSON default. 4-space is common in Python projects following PEP 8. Either works; consistency within a project matters more than the choice itself.
Free tool
Try the JSON Formatter & Validator
Use our free json formatter & validator to calculate results instantly — no signup required.
Open JSON Formatter & Validator →