Why Your JSON Is Invalid: The Three Errors Behind Most Failures
Almost every "unexpected token" error comes down to three things JavaScript allows and JSON does not.
Unexpected token } in JSON at position 247 is not a helpful error message. Here's what's almost certainly wrong.
1. Trailing commas
{
"name": "Ada",
"role": "engineer",
}
That comma after the last value is legal in JavaScript and illegal in JSON. It's the single most common cause, and it appears constantly in config files that someone edited by deleting the last entry.
2. Single quotes
{ 'name': 'Ada' }
JSON requires double quotes on both keys and string values. Single quotes are a JavaScript object literal, not JSON. This one bites hardest when you copy an object out of your editor and expect it to parse.
3. Comments
{
// the user's display name
"name": "Ada"
}
JSON has no comments. Not //, not /* */. Some parsers tolerate them (VS Code's "JSONC" for settings files), but JSON.parse does not, and neither will most APIs.
The pattern behind all three
Every one of these is valid JavaScript. JSON looks like JavaScript object syntax, which makes people assume it accepts the same things. It's a much stricter subset, deliberately — the whole point is that any language can parse it identically.
Two more that catch people out
Unquoted keys — {name: "Ada"} is a JS object, not JSON. Keys always need quotes.
NaN, Infinity and undefined — none of them exist in JSON. Serialise them as null or a string, or the receiving end will choke.
Finding the line, not the position
"Position 247" is useless in a 4,000-character document. Paste it into SwitchPDF JSON Formatter — it converts the parser's character offset into a line and column so you can jump straight to it, then beautifies the document once it's valid.
It runs entirely in your browser using the same JSON engine your code uses, so pasting a production API response or a config file with real hostnames carries no more risk than opening it in a text editor.
Duplicate keys: the silent one
{ "id": 1, "id": 2 }
This is technically valid JSON, and the last value wins. No error, no warning — just a value you didn't expect. If a field keeps coming through "wrong," check whether it appears twice.
Bottom line
Trailing comma, single quotes, comments. Check those three first and you'll resolve most invalid-JSON errors before you finish reading the stack trace.
Related articles
UUID v4 vs v7: Which One to Use as a Database Key
Random UUIDs fragment your index. v7 fixes it by putting a timestamp at the front. Here is the trade-off.
Base64 Explained: When to Use It and When Not To
Base64 is not encryption and it makes your data bigger. Here is what it is actually for.
Converting CSV to JSON Without Breaking Quoted Fields
Splitting on commas works until a value contains a comma. Here is what a real CSV parser handles that a split() does not.