If you've ever stared at a single wall of unreadable text and thought "this is JSON?", you're not alone. Minified or poorly formatted JSON is one of the most common pain points developers deal with daily. This guide covers every way to format JSON — from quick online tools to programmatic solutions in JavaScript and Python.
What does formatting JSON mean?
Formatting JSON (also called JSON beautifying, JSON pretty-printing, or JSON indentation) means adding consistent whitespace — newlines and indentation — to make the structure human-readable. It doesn't change the data, only its visual presentation.
Minified JSON (what APIs often send):
{"name":"Alice","age":28,"address":{"city":"Paris","zip":"75001"}}
Formatted JSON (what humans want to read):
{
"name": "Alice",
"age": 28,
"address": {
"city": "Paris",
"zip": "75001"
}
}
Method 1: Use an online JSON formatter (fastest)
The quickest way to format JSON is to paste it into an online formatter. No installation, no code — just paste and copy.
Try it free — JSON Formatter
Paste your JSON and get it formatted with syntax highlighting in one click. Free, no signup.
Method 2: Format JSON in VS Code
If you're already in VS Code, formatting JSON takes one keyboard shortcut:
- Windows/Linux:
Shift + Alt + F - Mac:
Shift + Option + F
Make sure your file is saved with a .json extension, or set the language mode to JSON first using Ctrl+K M.
Method 3: Format JSON in JavaScript
JavaScript's built-in JSON.stringify() handles formatting natively:
const data = { name: "Alice", age: 28 };
// Format with 2-space indentation
const formatted = JSON.stringify(data, null, 2);
console.log(formatted);
// Format with 4-space indentation
const formatted4 = JSON.stringify(data, null, 4);
// Minify (remove all whitespace)
const minified = JSON.stringify(data);
The third parameter of JSON.stringify() is the space argument. Pass a number for spaces, or " " for tab indentation.
Method 4: Format JSON in Python
import json
# Parse a JSON string
data = json.loads('{"name":"Alice","age":28}')
# Format with indent
formatted = json.dumps(data, indent=2)
print(formatted)
# Format from file
with open('data.json', 'r') as f:
data = json.load(f)
with open('formatted.json', 'w') as f:
json.dump(data, f, indent=2)
Method 5: Format JSON from the command line
Using jq (the most popular JSON CLI tool):
# Format a JSON file
jq . data.json
# Format and save
jq . data.json > formatted.json
# Format JSON from a curl response
curl https://api.example.com/users | jq .
On macOS: brew install jq. On Ubuntu: apt install jq.
Using Python's built-in JSON module (no installation needed):
cat data.json | python3 -m json.tool
Most common JSON formatting errors
Trailing commas
// ✗ Invalid — trailing comma after last item
{ "name": "Alice", "age": 28, }
// ✓ Valid
{ "name": "Alice", "age": 28 }
Single quotes instead of double quotes
// ✗ Invalid — JSON requires double quotes
{ 'name': 'Alice' }
// ✓ Valid
{ "name": "Alice" }
Unquoted keys
// ✗ Invalid — keys must be quoted strings
{ name: "Alice" }
// ✓ Valid
{ "name": "Alice" }
Comments
JSON does not support comments. If you need comments in config files, switch to YAML or JSON5.
JSON formatting best practices
- Use 2-space indentation — standard in JavaScript/Node.js projects
- Use 4-space indentation — standard in Python projects
- Keep keys lowercase — camelCase (
firstName) or snake_case (first_name), but be consistent - Sort keys alphabetically — makes diffs easier to read
- Always validate after formatting — use a validator to catch errors