JSON is everywhere — API responses, config files, database exports, webhook payloads. But JSON from production systems is often minified, inconsistently formatted or downright broken. This guide covers everything you need to work with JSON effectively.
What does JSON formatting mean?
Minified JSON (what APIs send):
{"name":"Alice","age":30,"address":{"city":"Bengaluru","zip":"560001"}}
Formatted JSON (what humans read):
{
"name": "Alice",
"age": 30,
"address": {
"city": "Bengaluru",
"zip": "560001"
}
}
Same data. The formatted version takes more bytes but is dramatically easier to read and debug.
How to format JSON in Python
import json
# Pretty-print with 2-space indent
data = {"name": "Alice", "age": 30, "active": True}
formatted = json.dumps(data, indent=2, ensure_ascii=False)
print(formatted)
# Sort keys alphabetically
sorted_json = json.dumps(data, indent=2, sort_keys=True)
print(sorted_json)
How to minify JSON in Python
import json
with open('config.json') as f:
data = json.load(f)
# Remove all whitespace
minified = json.dumps(data, separators=(',', ':'))
print(f"Original: {len(open('config.json').read())} bytes")
print(f"Minified: {len(minified)} bytes")
How to validate JSON and find errors
The most common JSON errors:
- Trailing commas —
{"a": 1, "b": 2,}— the last comma before}is invalid - Single quotes — JSON requires double quotes, not single quotes
- Unquoted keys —
{name: "Alice"}is JavaScript, not JSON - Missing commas — forgetting a comma between items in an array or object
- Unterminated strings — a string missing its closing
"
import json
json_string = '{"name": "Alice", "age": 30,}' # trailing comma
try:
data = json.loads(json_string)
print("Valid JSON")
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e.msg}")
print(f"Line {e.lineno}, Column {e.colno}")
💡 Quick fix: If you're getting a JSON error but can't see what's wrong, paste it into Sylvaera's JSON Formatter. It gives you the exact line number, column and a plain English fix suggestion.
Sorting JSON keys
Sorting JSON keys alphabetically makes it easier to compare two JSON objects and produces consistent git diffs. When reviewing API response changes, sorted keys show you exactly what fields changed:
import json
data = {"zebra": 1, "apple": 2, "mango": 3}
sorted_json = json.dumps(data, indent=2, sort_keys=True)
# Result:
# {
# "apple": 2,
# "mango": 3,
# "zebra": 1
# }
When to format vs minify
- Format — when debugging, reading API responses, writing config files, code review
- Minify — when sending over a network, storing in a database, embedding in HTML
- Sort keys — when comparing two JSON objects, storing in version control
- Validate — always before importing, before sending to an API, after any manual edits
Never manually edit minified JSON. Always format it first, make your changes, then minify again if needed.
Try JSON Formatter & Validator — Free
Format, minify, validate and sort JSON instantly. Get exact line numbers and plain English fix suggestions for errors. Zero AI cost — instant results.
Open JSON Formatter & Validator →