How to Read and Clean Up Messy JSON
Raw JSON from APIs, log files, and developer tools is usually minified — all on one line, no indentation. Debugging an object with 50 fields when every key-value pair is squeezed into a single line is painful. Formatting it first takes three seconds and makes the structure immediately readable.
What formatting does
JSON formatting (also called prettifying or beautifying) adds indentation and line breaks to make the structure visible. The content doesn't change — the same data is there, just arranged so your eyes can navigate it:
// Before formatting (minified)
{"user":{"id":1234,"name":"Alice","roles":["admin","editor"],"active":true}}
// After formatting
{
"user": {
"id": 1234,
"name": "Alice",
"roles": [
"admin",
"editor"
],
"active": true
}
}
The nested structure is now visible at a glance. You can see that user is an object with a roles array. In the minified version, this required careful reading character by character.
Spotting structural problems after formatting
Formatting reveals problems that are invisible in minified JSON: unexpected nesting depth (a value that should be a string is actually an object), missing expected keys (a field you expected isn't there), duplicate keys (some parsers silently use the last value, others throw errors), and inconsistent types across records in an array.
Cleaning copied JSON
JSON copied from browser dev tools, log outputs, or documentation often has extra content: JavaScript object notation (without quotes on keys), trailing commas, comments, or template literals. These need to be cleaned before the JSON is valid:
- Remove any
//comments - Add quotes around unquoted keys
- Remove trailing commas
- Replace
undefinedwithnull - Replace single quotes with double quotes
Handling large JSON payloads
For very large JSON (megabytes with thousands of nested objects), formatting the entire thing isn't always useful — you need to navigate to the relevant section. Most code editors (VS Code, Sublime) handle large JSON well with their built-in formatters. Browser-based formatters have size limits. For very large payloads, using jq on the command line with a targeted query is faster than formatting the whole document.
Frequently Asked Questions
How do I make minified JSON readable? Paste it into a JSON formatter. The formatter adds indentation and line breaks, turning a single compressed line into a structured, readable document. Most online formatters do this instantly.
What is minified JSON? Minified JSON is JSON with all unnecessary whitespace removed — no spaces, no line breaks, everything compressed onto one line. This reduces file size for transmission but makes it very hard to read or debug without formatting first.
How do I pretty print JSON? Paste the JSON into a formatter and it outputs an indented, human-readable version. In code, JSON.stringify(data, null, 2) in JavaScript produces pretty-printed output with 2-space indentation.
Why is my JSON showing escaped quotes everywhere? This is double-encoded JSON — the JSON has been serialised as a string inside another JSON value. You will see \" instead of regular quotes. Parse the outer JSON first to extract the string, then parse that string again as JSON to get the actual object.
What is the difference between formatting and validating JSON? Formatting adds indentation and line breaks to make JSON readable but does not check for errors. Validating checks whether the JSON is syntactically correct. Validate first if the JSON might be broken, then format to inspect the structure.
How do I find errors in a large JSON file? Paste it into a JSON validator. A validator will point to the exact line and character position of the first syntax error. Without a validator, scanning a large JSON file by eye for a missing comma is impractical.
Can I format JSON in VS Code? Yes. Open a .json file and press Shift+Alt+F on Windows/Linux or Shift+Option+F on Mac. VS Code also highlights syntax errors inline as you type. For files not saved as .json, paste the content into an online formatter instead.
Why does formatted JSON look different from what I pasted? Formatters normalise whitespace and key ordering may vary by tool. The content itself — values, types, nesting — stays the same. If the structure looks different after formatting, check whether the input had syntax errors that the formatter silently corrected or rejected.