How to Read, Format and Fix JSON
Learn the structure of JSON, the syntax errors that break parsing, and a practical process for debugging malformed data.
JSON is a text format for structured data. You see it in APIs, configuration files, browser storage, webhooks, and logs. Because the syntax is intentionally small, most invalid JSON problems come from a short list of mistakes.
The building blocks
A JSON object uses braces and contains name/value pairs. Arrays use brackets and contain ordered values. Values can be strings, numbers, objects, arrays, true, false, or null.
{
"name": "Probably Useful",
"categories": ["Web", "Finance"],
"published": true
}
Property names and strings use double quotes
This is valid: {"name":"Alex"}. This is not standard JSON: {'name':'Alex'}. JavaScript can accept syntax that JSON cannot.
No trailing commas
The last property or array item is not followed by a comma. Trailing commas are common in programming languages and a frequent reason copied JavaScript objects fail as JSON.
Comments are not part of standard JSON
// comments and /* comments */ are not valid standard JSON. Some configuration formats intentionally extend JSON, but a strict JSON parser will reject them.
Use formatting to reveal structure
Pretty-printing adds indentation and line breaks. It does not repair invalid data, but it makes nested objects and arrays much easier to inspect after the JSON parses successfully.
A practical debugging process
- Validate the smallest reproducible input.
- Check the character position from the parser error.
- Look just before that position for missing commas, quotes, braces, or brackets.
- Remove non-JSON constructs such as
undefinedor comments. - Format the valid result and verify the structure matches what the receiving system expects.
Minified JSON is still JSON
Removing whitespace can reduce size, but it does not make the data secure or unreadable to software. Do not treat minification as protection for secrets.
