Developer Guide
How to format JSON in JavaScript and Python
Pretty-printing for debugging and readable logs
JavaScript with JSON.stringify
Pass the parsed value as the first argument and an indentation count as the third argument. Two spaces is a common balance between readability and file size.
const payload = { user: "Ada", active: true };
const pretty = JSON.stringify(payload, null, 2);
console.log(pretty);
When starting with text, parse it first so malformed JSON fails before formatting:
const pretty = JSON.stringify(JSON.parse(rawJson), null, 2);
Python with json.dumps
Python's standard library provides json.dumps. The indent argument produces readable output, while sort_keys can make generated files stable for review.
import json
pretty = json.dumps(payload, indent=2, sort_keys=True)
print(pretty)
Validate before formatting
Formatting cannot repair a missing quote or trailing comma. Parse first, capture the error location, and then fix the source. You can paste the input into JSONLints Studio to inspect line and column details before applying a format.
Schema validation is a separate step
Valid JSON only guarantees correct syntax. A schema validator is still needed to enforce required properties, string formats, ranges, and application-specific rules.