All posts

Reading messy JSON — how to format, validate, and actually find the error

How to turn a wall of minified JSON into something readable, the mistakes that break it, and how to pinpoint the exact character that's wrong.

8 June 20264 min readBy Primova
jsondevdebugging

You copy a JSON response out of a network tab and it's one long line, four thousand characters wide, no spaces. Or worse: it won't parse at all, and the only feedback you get is Unexpected token } in JSON at position 2847 — which tells you almost nothing useful. Both of these are the same five-second fix with the right tool, so let's cover both, plus how to stop hitting the second one.

Pretty-printing: just add the structure back

Minified JSON — no whitespace, everything jammed onto one line — is how machines like to send it, because spaces are wasted bytes over the wire. It's miserable to read. Formatting (or "pretty-printing," or "beautifying") simply adds indentation and line breaks back so the structure becomes visible.

Same data, before and after formattingMINIFIED — unreadable{"user":{"id":7,"roles":["admin","editor"],"active":true}}FORMATTED — you can see the shape{"user": {"id": 7,"roles": ["admin", "editor"],"active": true }}

Nothing about the data changes — the two are the same JSON. Formatting is purely for your eyes. Once it's indented you can actually see what's nested inside what, where a key lives, and whether that array has three items or thirty.

Validating: finding the one bad character

The harder problem is when JSON is invalid and you have to find why. The culprits are boringly consistent — learn these five and you'll recognise almost every parse error on sight:

MistakeLooks likeFix
Trailing comma[1, 2, 3,]Drop the last comma
Single quotes{'a': 1}JSON needs double quotes
Missing comma{"a":1 "b":2}Separate items with ,
Unquoted key{a: 1}Keys must be "quoted"
Unescaped quote"she said "hi""Escape inner quotes: \"

The trouble is that a strict parser just reports the position where it gave up — which is often a bit after the real mistake. So position 2847 might point at the brace that revealed the problem, not the missing comma three lines up that caused it. What you actually want is to see the document laid out with the failing spot highlighted, so you can read the surrounding lines and spot the stray comma yourself.

Why this matters: JSON is unforgiving by design — there's no "mostly valid." One trailing comma and the entire payload is rejected. That strictness is what makes it reliable for machines, and exactly what makes a good error message worth its weight when you're debugging.

Doing both at once

Primova's JSON Formatter does the two jobs together. Paste your JSON and it formats instantly if it's valid; if it isn't, it tells you the line and column of the problem so you're looking at the right place instead of scanning the whole blob. From there you can:

  • Minify it again, to copy a compact version back into code.
  • Sort keys so two objects line up and become easy to compare.
  • Read a quick breakdown of how many keys it has and how deep the nesting goes.

It runs entirely in your browser, which matters more than it sounds: the JSON you're debugging is usually an API response with a token, an email, or a customer record in it. None of that should be pasted into a server you don't control — and here, none of it is. It never leaves your machine.

Paste a payload that's been bugging you into the JSON Formatter and you'll usually see the problem before you've finished reading this sentence.