If you've spent more than a week writing JavaScript, you've seen this error:
SyntaxError: Unexpected token '}' in JSON at position 42
Or maybe:
SyntaxError: Unexpected token o in JSON at position 1
Or the cousin:
SyntaxError: Unexpected end of JSON input
This is one of the most-Googled JSON errors in existence. It's a rite of passage for every developer who's ever called JSON.parse() on something that wasn't actually JSON. But the frustrating part isn't the error itself — it's that the browser tells you the character position, and you still can't figure out what's wrong.
Here's how to actually debug this thing.
What the Error Is Telling You
JSON.parse() expects a string that looks exactly like valid JSON. If the string has any deviation — a trailing comma, a single quote instead of double, an extra bracket, or a raw JavaScript object — it throws.
The error message gives you two pieces of information:
JSON.parse("{ invalid json }");
// SyntaxError: Unexpected token i in JSON at position 2
- Position 2: start counting from 0:
{= 0,= 1,i= 2 - Unexpected token
i: the parser hit the letteriwhere it expected a valid JSON value
But here's the thing — the position isn't always the exact problem. Sometimes it's the first character the parser couldn't understand, but the real mistake is earlier. Like a missing opening brace causes the parser to fail at a later closing brace.
The Most Common Causes
1. Single Quotes Instead of Double Quotes
This is the #1 cause. JSON requires double quotes for strings and keys. JavaScript objects don't.
// JavaScript object — works in JS, not in JSON.parse()
const obj = { name: 'John' };
JSON.parse("{ name: 'John' }");
// SyntaxError: Unexpected token n in JSON at position 2
// (position 2 is the 'n' in "name" — JSON expects "name" with double quotes)
// Correct JSON
JSON.parse('{ "name": "John" }');
2. Trailing Commas
JSON does not allow trailing commas after the last element.
JSON.parse('{ "name": "John", }');
// SyntaxError: Unexpected token } in JSON at position ...
Same goes for arrays:
JSON.parse('[1, 2, 3,]');
// SyntaxError: Unexpected token ] in JSON at position ...
3. You Passed an Object, Not a String
This one's sneaky. If you call JSON.parse() on something that's already a JavaScript object, you get "Unexpected token o in JSON at position 1".
Why o? Because JavaScript coerces the object to a string, and [object Object] starts with o at position 1 ([ = 0, o = 1).
const data = await fetch('/api/user').json(); // already parsed!
JSON.parse(data);
// SyntaxError: Unexpected token o in JSON at position 1
// You don't need JSON.parse() here — data is already an object
Same thing happens with undefined:
JSON.parse(undefined);
// SyntaxError: Unexpected token u in JSON at position 0
This usually means the variable you're parsing is undefined — the API call failed, or you forgot to await it.
4. Escaped Characters Gone Wrong
If your JSON string contains unescaped control characters or malformed escape sequences:
JSON.parse('"hello\nworld"');
// SyntaxError — newlines must be escaped as \n in JS string literals too
// But if the \n is already in the JSON data:
JSON.parse('"hello\\nworld"'); // correct
5. The BOM (Byte Order Mark)
Sometimes a server response includes a BOM character (\uFEFF) at the start of the file. It's invisible in most editors, but it breaks JSON.parse:
const response = '\uFEFF{"name":"John"}'; // BOM at position 0
JSON.parse(response);
// SyntaxError: Unexpected token in JSON at position 0
Fix: strip it before parsing:
JSON.parse(response.replace(/^\uFEFF/, ''));
The Quickest Way to Find the Bug
When you're staring at a 500-line JSON blob and the error says "position 342," counting characters manually is a waste of time. The fastest fix is to paste the JSON string into a tool that highlights the exact problem.
Use the FastUnix JSON Formatter — paste your raw JSON string, click format or validate, and it'll show you exactly where the syntax breaks. Red underline on the offending character, no counting required.
Here's the workflow when you hit the error:
- Copy the string that's failing in
JSON.parse() - Paste it into the JSON formatter
- Look for red highlights — they mark every syntax error
- Fix the issues (missing quotes, trailing commas, etc.)
- Copy the cleaned JSON back
For example, paste this:
{ name: 'John', age: 30, }
And the formatter immediately highlights:
name→ missing double quotes'John'→ single quotes instead of double, }→ trailing comma
Three fixes, ten seconds. Much faster than counting positions in your head.
Real-World Scenario: Debugging an API Response
You're building a React app and getting data from an API:
const res = await fetch('/api/users');
const text = await res.text(); // log the raw string first!
console.log(text); // inspect it
Always log the raw response as text before parsing. res.json() will throw the same error, but you won't see the string. By calling res.text() first, you can see exactly what the server returned — and paste it into a formatter if it looks off.
Common things you'll find:
- The server returned HTML (a 404 page) instead of JSON
- The server returned
nullor"null"(yes, there's a difference) - The JSON is truncated (a proxy timeout)
- The JSON has a BOM character
Does This Error Always Mean Invalid JSON?
Not quite. Sometimes JSON.parse() is the wrong function entirely. If you're working with JSON5 (which allows comments, trailing commas, and single quotes), the standard JSON.parse() will reject valid JSON5. In that case, use a JSON5 parser instead.
// This is valid JSON5, NOT valid JSON
const json5 = "{ name: 'John', /* comment */ }";
JSON.parse(json5); // fails
// Use a JSON5 parser instead
import JSON5 from 'json5';
JSON5.parse(json5); // works
But for 95% of cases, the problem is one of the five causes listed above.
Wrapping Up
The "Unexpected token" error is frustrating because the position hint rarely points to the actual root cause. Instead of counting characters or staring at a wall of text, isolate the raw string and run it through a validator.
The fix is almost always one of:
- Wrap keys in double quotes
- Replace single quotes with double quotes
- Remove trailing commas
- Check you're passing a string, not an object
- Strip the BOM character
Next time you see that error in your console, paste the string into the FastUnix JSON Formatter, fix what's highlighted, and move on with your day.