JSON Validation Errors Explained: What They Mean and How to Fix Them
You copy a JSON payload, paste it into your code, and your application crashes with "SyntaxError: Unexpected token". The error message tells you position 247 — but you're looking at a 500-character blob with no idea where position 247 even is. This guide will walk you through every common JSON error, what causes it, and exactly how to fix it.
The Most Common JSON Error: Trailing Commas
If you've been writing JavaScript before working with JSON, this one will catch you out. JavaScript allows trailing commas in arrays and objects. JSON does not. It's one of the most frequent validation failures developers encounter.
The fix is simple: remove the comma after the last item in every object and array. But when you're editing a large config file with dozens of entries, finding that one rogue comma manually is painful. A validator finds it in under a second.
Single Quotes Instead of Double Quotes
JSON requires double quotes. Full stop. Not single quotes, not backticks. This trips up developers who write Python dictionaries or JavaScript object literals and then try to use them as JSON.
This is especially common when you copy data from a Python REPL or a JavaScript console. Both print objects with single quotes by default. Always convert to double quotes before treating a string as JSON.
' to ". But be careful — this can break strings that contain apostrophes. A validator is safer for confirming the fix is correct.Unquoted Keys
JavaScript objects don't require quotes around keys. JSON does. This is another area where copying JS object literals into JSON contexts causes failures.
The parser sees name without quotes and doesn't know what to do with it — it expects either a string literal (starting with ") or the end of the object (}). The resulting error message — "Unexpected token n" — is cryptic until you know the cause.
Comments in JSON — They're Not Allowed
Here's one that surprises a lot of developers. JSON does not support comments. No //, no /* */, nothing. If your JSON contains comments (common in config files written with a JSON-like syntax), it will fail standard validation.
If you need comments in config files, consider using JSONC (JSON with Comments, supported by VS Code and some tools) or YAML instead. For standard JSON used in APIs and data exchange, remove all comments before sending.
In our experience, this error is most common in teams that use JSON for configuration files and get used to adding explanatory comments. When that config file is then parsed by a standard JSON library, it breaks.
Unescaped Special Characters
Certain characters inside JSON string values must be escaped with a backslash. The most common ones developers miss are double quotes, backslashes, and newlines.
Newlines within strings are another frequent issue. A literal line break inside a JSON string value is invalid — it must be represented as \n. This often happens when multi-line text is programmatically inserted into a JSON string without proper escaping.
JavaScript-Only Values That Aren't Valid JSON
JSON supports exactly six data types: strings, numbers, booleans (true/false), null, objects, and arrays. Any JavaScript-specific value will fail JSON validation.
If you're serialising a JavaScript object to JSON using JSON.stringify(), these values are automatically handled — undefined properties are dropped and NaN becomes null. But if you're hand-writing JSON, you'll need to replace these manually.
The actionable takeaway: if your JSON is generated by code, always use JSON.stringify(). If you're writing it by hand, stick to the six valid types and run it through a validator before using it.
Duplicate Keys — Silent but Dangerous
Technically, the JSON spec doesn't prohibit duplicate keys. But in practice, most parsers will silently drop all but the last occurrence — which means data loss you might not notice for a long time.
In this example, most parsers will return id: 2 and silently discard the first id: 1. If your application logic depends on the first value, it will fail in unexpected ways. A good validator flags this as a warning even when it doesn't technically cause a parse error.
We recommend treating duplicate key warnings as errors, especially in production configs and API responses.
JSON Validation in Different Languages
Every major programming language has a built-in or standard library method for JSON validation. Knowing your platform's approach saves you from writing manual checks.
Validate Your JSON Right Now
Paste your JSON and get an instant validity check with exact error location — no install, no login.
🔍 Open JSON Validator →Recommended Hosting
Hostinger
If you are building a website for your tools, blog, or store, reliable hosting matters for speed and uptime. Hostinger is a popular option used worldwide.
Visit Hostinger →Disclosure: This is a sponsored link.