How to Automatically Fix "Unexpected Token" JSON Errors
By M Nouman Saeed · Published on April 21, 2026 · Updated July 28, 2026 · 7 min read
It happens to every developer: you're working with an API or a config file, you try to parse a JSON string, and everything crashes with a cryptic error message: Uncaught SyntaxError: Unexpected token ... in JSON at position 0.
JSON (JavaScript Object Notation) is notoriously strict. Unlike JavaScript objects, JSON requires a very specific syntax. A single misplaced character can break your entire data pipeline.
💡 Pro-Tip: Auto-Fix JSON Errors
You don't need to manually hunt for missing commas or unquoted keys. Paste your broken JSON into our JSON formatting tool and it will automatically repair syntax errors. (If you just need to strip invisible characters, you can also use our Text Cleaner).
Reading the Error Message
Before fixing anything, it helps to understand what the browser is actually telling you. Unexpected token X in JSON at position N means the parser was reading character-by-character and hit character X at index N, where it did not expect to find it based on the JSON grammar rules so far.
A position of 0usually means the very first character is the problem — often because the string isn't JSON at all (for example, your API returned an HTML error page instead of data), or it starts with a stray invisible character.
Common JSON Syntax Culprits
Most "Unexpected Token" errors are caused by one of a handful of common mistakes that are often hard to spot with the naked eye.
1. The Trailing Comma
This is the most frequent error. JavaScript allows trailing commas in objects and arrays, but JSON does not.
{ "name": "CleanTextLabs", "active": true, }
Notice the comma after "true"—that will cause a parse error.
2. Single Quotes Instead of Double Quotes
In JSON, all keys and string values must use double quotes ("). Single quotes (') are invalid and will throw an error immediately.
3. Unquoted Keys
Standard JavaScript objects allow you to omit quotes for keys, but JSON requires every key to be wrapped in double quotes.
4. Comments
Developers frequently paste in config-file style JSON that includes // or /* */ comments. Standard JSON parsers do not support comments at all, and will fail the moment they encounter one.
5. Invisible Unicode Characters
The hardest bug to spot: a zero-width space or non-breaking space copied in from a PDF or chat app, sitting silently inside your JSON string. It's invisible on screen but very real to the parser. See our guide to finding and removing zero-width spaces for how to catch these.
How to Debug and Fix Invalid JSON
When you're dealing with a large JSON payload (hundreds or thousands of lines), finding a missing quote or a trailing comma is like looking for a needle in a haystack.
The best way to debug is to use a validator that provides clear error locations. Instead of just saying "Unexpected token", a good tool will tell you exactly which line and character index the parser failed on. Pairing that with a properly indented, beautified view (see our JSON formatting guide) makes it far easier to visually scan for the exact bracket or comma at fault.
Automating the Fix
Don't waste time hunting for commas manually. We built our Online JSON Formatter & Validator to handle this exact scenario.
When you paste messy or invalid JSON into our tool, it instantly runs a validation pass. If there's an error, it highlights the exact spot. Even better, our "Beautify" feature often automatically corrects common formatting issues, allowing you to get back to coding without the headache.
Frequently Asked Questions
What does 'Unexpected token in JSON at position 0' mean?
Position 0 means the parser failed on the very first character. This almost always means the string isn't JSON at all — for example, it's empty, it's HTML (an error page returned instead of data), or it starts with a stray character like a BOM before the opening brace.
Why does my JSON look correct but still fail to parse?
The most common invisible cause is a hidden Unicode character, such as a zero-width space or a non-breaking space, copied in from a PDF, chat app, or word processor. The text looks identical on screen but the parser sees an extra, invalid character. See our guide on removing zero-width spaces for how to catch these.
Can comments be used inside JSON?
No. Standard JSON has no concept of comments, unlike JSON5 or JSONC. Adding '// a comment' or '/* a comment */' inside a JSON string will cause a parse error, even though it's valid in many config-file dialects that look similar.
What's the difference between JSON.parse errors and JSON.stringify errors?
JSON.parse errors happen when converting a JSON string into an object, and are almost always caused by invalid syntax in the string. JSON.stringify errors happen when converting an object into a string, and are usually caused by circular references or unsupported types like functions inside the object.
Struggling with a JSON parse error?
Paste your raw data into our validator and find the bug instantly.
Go to JSON Formatter