How to Fix Invalid JSON: Common Errors & Solutions
Diagnose and fix the 10 most common JSON errors developers encounter. Each error includes before/after code examples and a clear explanation of why it happens.
Why JSON Parsing Fails
JSON looks simple — and it is — but its strictness is exactly what trips people up. Unlike JavaScript object literals, JSON has zero tolerance for syntax shortcuts. No trailing commas. No single quotes. No comments. Every deviation produces a parse error, and the error messages are often unhelpful: Unexpected token or JSON.parse: expected ',' or '}' at line 1.
This guide covers the 10 most common JSON errors developers encounter in the wild. Each includes the broken JSON, why the parser rejects it, and the corrected version. Bookmark this page — you'll come back to it.
Try it yourself: Paste broken JSON into our JSON Repair tool to auto-fix most of these errors instantly.
Error 1: Trailing Comma
This is the single most common JSON error. JavaScript allows trailing commas in objects and arrays, so developers naturally write JSON the same way. But JSON doesn't permit them.
Broken:
{
"name": "Alice",
"age": 30,
}Fixed:
{
"name": "Alice",
"age": 30
}The trailing comma after 30 causes the parser to expect another key-value pair. When it finds a closing brace instead, it throws. Remove the comma after the last item in every object and array.
Error 2: Single Quotes Instead of Double Quotes
JSON requires double quotes for both keys and string values. Single quotes are a JavaScript-ism that the JSON parser rejects outright.
Broken:
{
'name': 'Alice',
'city': 'Portland'
}Fixed:
{
"name": "Alice",
"city": "Portland"
}This happens most often when copying object literals from JavaScript code or from Python dictionaries (which also use single quotes). A find-and-replace from ' to " fixes it, but be careful with strings that contain apostrophes — those need escaping.
Error 3: Unquoted Keys
JavaScript lets you write { name: "Alice" } without quoting the key. JSON does not. Every key must be a double-quoted string.
Broken:
{
name: "Alice",
age: 30
}Fixed:
{
"name": "Alice",
"age": 30
}This usually surfaces when someone hand-writes JSON or copies a JavaScript object without converting it. Some editors and API debugging tools will happily display unquoted keys, masking the problem until you try to parse the file elsewhere.
Error 4: Comments in JSON
JSON does not support comments — not // single-line, not /* */ block comments. This is one of the most frustrating limitations of the format, especially for configuration files.
Broken:
{
// Database settings
"host": "localhost",
"port": 5432 /* default PostgreSQL port */
}Fixed:
{
"host": "localhost",
"port": 5432
}If you need comments in config files, consider using JSONC (JSON with Comments), which VS Code and TypeScript support natively, or switch to YAML or TOML. For standard JSON, strip all comments before parsing.
Error 5: Missing Comma Between Items
When adding a new field to a JSON object, it's easy to forget the comma separating it from the previous field. The parser sees two consecutive values with no separator and chokes.
Broken:
{
"name": "Alice"
"age": 30
}Fixed:
{
"name": "Alice",
"age": 30
}The error message usually points to the line after the missing comma, which can be misleading. If you see Unexpected string or Expected comma, look at the line above the reported position.
Error 6: Missing Closing Bracket or Brace
Mismatched brackets are hard to spot in large JSON files. A missing ] or } causes the parser to read past the intended end of a structure, producing confusing errors far from the actual problem.
Broken:
{
"users": [
{ "name": "Alice" },
{ "name": "Bob" }
}Fixed:
{
"users": [
{ "name": "Alice" },
{ "name": "Bob" }
]
}The fix: use an editor with bracket matching (VS Code highlights matching pairs) or a JSON Validator that reports the exact location of the mismatch.
Error 7: undefined, NaN, or Infinity
These are valid JavaScript values but have no JSON representation.JSON.stringify silently converts undefined to null (or omits the key entirely), but if you manually write these values, the parser rejects them.
Broken:
{
"score": NaN,
"callback": undefined,
"limit": Infinity
}Fixed:
{
"score": null,
"callback": null,
"limit": null
}Replace undefined and NaN with null, or omit the key entirely if the absence of a value is semantically appropriate. For Infinity, consider using a sentinel number or a string representation like "Infinity" and handling it in your application code.
Error 8: Extra Comma After Last Array Item
This is the array version of Error 1. It shows up constantly in hand-edited JSON files, especially after deleting or reordering items.
Broken:
{
"colors": [
"red",
"green",
"blue",
]
}Fixed:
{
"colors": [
"red",
"green",
"blue"
]
}The pattern is the same: the comma after "blue" tells the parser to expect another element. It finds ] instead and reports an error. When removing items from a JSON array, always check whether the new last item has a trailing comma.
Error 9: Smart Quotes (Curly Quotes)
This one is sneaky. When you copy JSON from a Word document, Slack message, or a blog post, your system may silently replace straight quotes (") with typographic "smart" quotes (\u201C and \u201D). They look almost identical but are completely different characters.
Broken:
{
“name”: “Alice”,
“city”: “Portland”
}Fixed:
{
"name": "Alice",
"city": "Portland"
}The error message is usually Unexpected token at position 1 or 2, which is baffling until you realize the quote characters are wrong. The fix: replace all curly quotes with straight double quotes. In a pinch, retype the quotes manually in a plain-text editor.
To prevent this, always use a code editor (not a rich-text editor) to write JSON. Disable "smart quotes" in your OS keyboard settings if you frequently paste between applications.
Error 10: BOM (Byte Order Mark) Characters
A BOM is an invisible character (U+FEFF) that some Windows text editors insert at the beginning of a file to indicate encoding. JSON parsers see it as an unexpected character before the opening brace and fail immediately.
Symptoms:
SyntaxError: Unexpected token in JSON at position 0You can't see the BOM in most editors — it's truly invisible. To detect it, open the file in a hex editor and look for the bytes EF BB BF (UTF-8 BOM) at the very beginning.
How to fix:
- In VS Code, click the encoding indicator in the status bar and choose "Save with Encoding" → "UTF-8" (without BOM).
- On the command line:
sed -i '1s/^\xEF\xBB\xBF//' file.json - In Node.js, strip it before parsing:
const clean = text.replace(/^\uFEFF/, "");
Quick Reference: All 10 Errors
| # | Error | Cause | Fix |
|---|---|---|---|
| 1 | Trailing comma | Comma after last property/element | Remove the trailing comma |
| 2 | Single quotes | Used ' instead of " | Replace with double quotes |
| 3 | Unquoted keys | Keys without double quotes | Wrap all keys in double quotes |
| 4 | Comments | // or /* */ in JSON | Remove all comments |
| 5 | Missing comma | No comma between items | Add comma between items |
| 6 | Missing bracket/brace | Unclosed [ or { | Add the missing closing character |
| 7 | undefined / NaN / Infinity | JS-only values in JSON | Replace with null or a string |
| 8 | Extra comma in array | Trailing comma in array | Remove the trailing comma |
| 9 | Smart quotes | Curly quotes from Word/Slack | Replace with straight double quotes |
| 10 | BOM character | Invisible U+FEFF at file start | Re-save as UTF-8 without BOM |
Preventing JSON Errors
Fixing errors is good. Not creating them in the first place is better. Here are the habits that keep JSON clean:
- Never hand-write JSON. Generate it from code using
JSON.stringifyor your language's equivalent. Serializers produce valid JSON by definition. - Use a JSON-aware editor. VS Code, JetBrains IDEs, and Sublime Text all highlight JSON syntax errors in real time. If you see a red squiggly, fix it before saving.
- Validate in CI. Add a validation step to your build pipeline that parses all JSON files and fails on errors. A single line of shell script does it:
find . -name "*.json" -exec python -m json.tool {} \; - Use JSONC for config. If you need comments, use JSONC (
.jsoncextension) where supported. TypeScript, VS Code, and many other tools handle it natively.
When to Use Automated Repair
Sometimes the JSON you receive is out of your control — an API response from a poorly-built service, a file exported by legacy software, or a chunk pasted by a non-technical team member. In these cases, manual repair is tedious and error-prone.
Automated repair tools can fix most of the errors listed above in one pass. They parse the broken input, infer the intended structure, and produce valid JSON. This works well for common issues like trailing commas, single quotes, and unquoted keys.
The tradeoff is that automated repair makes assumptions. If the JSON is badly malformed — missing large sections or structurally ambiguous — the tool might guess wrong. Always validate the repaired output before using it in production.
Try it yourself: Paste broken JSON into our JSON Repair tool to auto-fix common errors, or use the JSON Validator to pinpoint exactly where the problem is.