How to Format JSON and Make It Readable
To format JSON, parse the complete value and serialize it with consistent indentation. Two spaces are a useful starting point for nested data. Formatting reveals structure; it cannot decide what malformed data was intended to mean. Fix syntax errors first and keep the original for comparison.
Read the structure before changing it
Objects group named properties inside braces; arrays group ordered values inside brackets. Indentation makes their boundaries easier to follow. It does not turn a string into a number or prove that a required field exists. For an API response, locate the property you need and follow its surrounding objects and arrays instead of scanning one long line.
In a local editor, select JSON as the document language and use its format command. In JavaScript, the example below parses a small fixture and prints it with two spaces. A parse error means the input must be repaired before this operation can finish.
const source = '{"items":[{"name":"demo","active":true}]}';
JSON.stringify(JSON.parse(source), null, 2);
// Result:
{
"items": [
{
"name": "demo",
"active": true
}
]
}Format and inspect in four steps
1. Copy only the JSON value, excluding HTTP headers, code fences and variable assignments. Keep a separate original.
2. Run a syntax check. If it fails, inspect quotes, commas and closing brackets near and before the reported location.
3. Choose two spaces, four spaces or tabs to match the surrounding project. Expand nested objects and check array boundaries.
4. Compare values and types with the original before saving or copying the result. Use minification later if the destination needs compact text.
Changes indentation cannot prevent
JavaScript parses numbers into its numeric representation. An unquoted identifier beyond the safe integer range can be rounded; preserve such identifiers as strings at their source. Duplicate property names are also unsafe because parsing may retain only the later value. A formatter cannot recover a discarded value.
Do not use ordinary text whitespace cleanup on JSON: it can alter spaces inside strings. Formatting is also not schema validation. A readable document can still contain the wrong fields, types or application values.
Follow along in Flekify
To inspect the nested example while following these steps, open JSON Formatter. Choose indentation and compare the tree with the source. The tool processes the input locally in your browser; use JSON Validator when the first obstacle is a syntax error.