How to Format and Validate JSON Online
Minified JSON is unreadable. A single-line blob from an API response or config file is impossible to debug without formatting it first. Here's how to format JSON instantly, validate it, and fix the common errors that make parsers choke.
What JSON formatting does
Formatting (also called pretty-printing) takes this:
{"name":"John Doe","age":30,"address":{"city":"New York","zip":"10001"},"tags":["developer","writer"]}
And produces this:
{
"name": "John Doe",
"age": 30,
"address": {
"city": "New York",
"zip": "10001"
},
"tags": [
"developer",
"writer"
]
}
Same data, different whitespace — the JSON is still valid in both forms.
Formatting in the browser
Paste minified JSON into the formatter, click format, and get indented output. Most formatters also validate as you type, highlighting the exact line where a syntax error occurs. This is the fastest path for one-off debugging.
Formatting in JavaScript
const minified = '{"name":"John Doe","age":30}';
const formatted = JSON.stringify(JSON.parse(minified), null, 2);
console.log(formatted);
JSON.stringify(value, replacer, space) — the third argument is the indent. 2 gives 2-space indentation; '\t' gives tabs.
To format a JSON file in Node.js:
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('data.json', 'utf8'));
fs.writeFileSync('data.json', JSON.stringify(data, null, 2));
Formatting in Python
import json
minified = '{"name": "John Doe", "age": 30}'
formatted = json.dumps(json.loads(minified), indent=2)
print(formatted)
Format a file in place:
import json
with open('data.json', 'r') as f:
data = json.load(f)
with open('data.json', 'w') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
ensure_ascii=False preserves Unicode characters (é, ñ, 你好) instead of escaping them to \uXXXX.
Command line: jq
jq is a command-line JSON processor. Format a file:
# Install
brew install jq # macOS
sudo apt install jq # Ubuntu
# Pretty-print
jq . data.json
# Minify
jq -c . data.json
# Format a piped API response
curl -s https://api.example.com/data | jq .
Python one-liner (no install needed):
python3 -m json.tool data.json
Common JSON errors and how to fix them
Trailing comma:
{
"name": "John",
"age": 30, ← error
}
JSON does not allow trailing commas. Remove the last comma before } or ].
Single quotes instead of double quotes:
{'name': 'John'} ← error
JSON requires double quotes for all strings and keys. Replace ' with ".
Unquoted keys:
{name: "John"} ← error
JavaScript object literal syntax is not valid JSON. Keys must be quoted: {"name": "John"}.
Comments:
{
// This is the user object
"name": "John"
}
JSON does not support comments. Remove them, or switch to JSONC (JSON with Comments) format if your tool supports it.
Undefined or NaN values:
{"value": undefined} ← error
{"value": NaN} ← error
undefined, NaN, and Infinity are JavaScript values, not JSON values. Replace with null, 0, or remove the field.
Unescaped special characters in strings:
{"message": "He said "hello""} ← error
Double quotes inside strings must be escaped: {"message": "He said \"hello\""}.
Newlines in strings also need escaping: \n not a literal line break.
Validating programmatically
function isValidJson(str) {
try {
JSON.parse(str);
return true;
} catch {
return false;
}
}
import json
def is_valid_json(s: str) -> bool:
try:
json.loads(s)
return True
except json.JSONDecodeError:
return False
For production use with detailed error messages, both JSON.parse (JS) and json.loads (Python) throw exceptions with the position of the error.
Sorting keys
Alphabetically sorted keys make diff comparisons much cleaner in version control:
const sorted = JSON.stringify(data, Object.keys(data).sort(), 2);
json.dumps(data, sort_keys=True, indent=2)
Key takeaways
JSON.stringify(data, null, 2)in JavaScript andjson.dumps(data, indent=2)in Python format any object.jq .is the fastest command-line formatter — also works great withcurloutput.- The five most common JSON errors: trailing commas, single quotes, unquoted keys, comments, and unescaped characters.
- Use
ensure_ascii=Falsein Python to keep Unicode readable. - Sort keys with
sort_keys=True/Object.keys().sort()for cleaner diffs.