Utility

How to Validate JSON and Find Syntax Errors Fast

A single missing comma can break an entire JSON file, and most error messages don't tell you where. Here's how a proper validator pinpoints the exact line and column.

๐Ÿ“… Jul 30, 2026ยทโฑ๏ธ 4 min readยทโœ๏ธ Cikal Studio Labs
๐Ÿงพ

Why "Invalid JSON" Isn't a Useful Error on Its Own

Anyone who has pasted a large JSON payload into a parser and gotten back nothing more than "Unexpected token" knows the frustration โ€” the message confirms something is wrong without saying where. For a 5-line object that's a minor annoyance; for a 500-line API response, it can mean scanning the whole thing by eye, bracket by bracket, to find one missing comma. A validator is only as useful as the location it can point to.

Why Browser Error Messages Aren't Reliable Enough

Every browser exposes JSON parsing through the same JSON.parse() function, but the error messages it throws are not standardized โ€” they vary by JavaScript engine and even by version of the same engine. Older engines included a numeric character position in the message ("Unexpected token } in JSON at position 45"); newer versions of some engines have moved to showing a text snippet instead, without any explicit position at all. A tool that tries to extract a line and column by pattern-matching those messages will work today and quietly break the next time a browser updates its wording.

A More Reliable Approach: Track Position While Parsing

The fix is to stop depending on the error message entirely. Instead, a small hand-written scanner walks the JSON text character by character, following the same grammar rules as any JSON parser โ€” an object needs a quoted key, then a colon, then a value, then a comma or closing brace, and so on โ€” while keeping a running character index. The moment something doesn't match what the grammar expects, that index is exactly where the document stopped making sense, independent of whatever wording any particular browser would have used. Converting that index into a line and column is simple from there: count the newlines before it for the line number, and measure the distance back to the last newline for the column.

๐Ÿ’ก Practical tip: The most common JSON errors are trailing commas (valid in JavaScript object literals, invalid in JSON) and missing quotes around keys โ€” both immediately obvious once you're pointed at the exact character instead of scanning the whole file.

What Validation Catches That a Successful Parse Doesn't

It's worth remembering that valid JSON and correct JSON aren't the same thing โ€” a validator confirms the syntax is well-formed, not that the data means what you intended. A JSON file with a typo'd key name, or a number stored as a string, will parse without any error at all. Syntax validation is the first filter, catching the mechanical mistakes (a stray comma, an unclosed bracket, an unescaped quote) that would otherwise crash whatever system consumes the file โ€” not a substitute for checking the actual content against whatever schema or expectations that system has.