How to Validate JSON and Fix Common Errors
· 5 min read
JSON looks simple until a parser throws "Unexpected token" and points at a line that seems perfectly fine. The trouble is that JSON is far stricter than the JavaScript object literals most developers are used to typing. Once you know the handful of rules the spec enforces, almost every error becomes obvious. Run your payload through a JSON Validator to get the exact line and column, then work through the list below.
Trailing commas
This is the single most common JSON error. JavaScript happily accepts a comma after the last element of an array or object, but JSON forbids it.
The fix is to delete the comma before any closing brace or bracket. If you assemble JSON by concatenating strings in code, this is usually where the bug lives: you append "item," in a loop and never trim the final one. Build an array and serialize it instead of hand-stitching the text.
Single quotes and unquoted keys
JSON requires double quotes around every string and every key. These are all invalid:
- Keys without quotes, like name: "Sam"
- Strings in single quotes, like 'hello'
- Smart or curly quotes pasted from a word processor
The last one is sneaky: the text looks quoted but the characters are typographic quotes, not ASCII double quotes. If a string looks correct but still fails, retype the quotes by hand.
Unescaped characters
Inside a JSON string, certain characters must be escaped with a backslash: double quotes, backslashes themselves, and control characters like newlines and tabs. A literal line break inside a string is illegal; it must be written as a backslash followed by n. Windows file paths bite people here because each backslash needs to be doubled. If you are embedding HTML or another JSON document inside a string, escape every interior quote.
Byte order marks and hidden characters
If the very first character of your file is invalid but you cannot see anything wrong, you probably have a UTF-8 byte order mark (BOM) at the start. Many editors add it silently. Strict parsers reject it. Save the file as UTF-8 without BOM, or strip the first three bytes. Non-breaking spaces and zero-width characters cause similar invisible failures.
Comments
JSON has no comments. Anything starting with two slashes or a slash-star block will fail. Configuration formats like JSONC and JSON5 allow them, but plain JSON does not. Remove comments before parsing, or convert the file to a format that supports them.
Numbers and values
JSON numbers cannot have leading zeros, trailing decimal points, or a plus sign on the exponent edge cases. NaN and Infinity are not valid JSON values. The literals true, false, and null must be lowercase. A common mistake is wrapping a number in quotes when a number was expected downstream, which validates fine but breaks the consumer.
A reliable workflow
When JSON fails, do not eyeball it. Paste it into a validator that reports the precise position of the first error, fix that one issue, and revalidate, because parsers stop at the first problem and there may be several. Once it parses, run it through a JSON Formatter to pretty-print and indent the structure so nesting mistakes become visible at a glance.
Everything here runs entirely in your browser. Your JSON, which often contains API keys, tokens, or customer data, is never uploaded to a server. That matters when you are debugging a production payload and cannot risk pasting secrets into an unknown backend.
Master these six categories and you will resolve the vast majority of JSON errors in seconds rather than minutes.