Why Your JSON Keeps Breaking — A Developer's Guide to Fixing JSON Errors
It happens to everyone. You copy JSON from somewhere, paste it into your code or config, and suddenly your application throws a cryptic SyntaxError: Unexpected token. You stare at the JSON. It looks fine. But something in there is wrong, and the error message isn't telling you much. This guide covers every common JSON error, why it happens, and exactly how to fix it — so the next time you see that error, you know where to look first.
The Golden Rules of Valid JSON
Before diving into specific errors, let's establish the non-negotiable rules of the JSON specification. Break any of these and your JSON is invalid — no exceptions:
- All string values and all object keys must use double quotes — never single quotes
- No trailing commas after the last item in an object or array
- No comments of any kind — not //, not /* */
- Keys in objects must be strings — unquoted identifiers are not allowed
- Numbers cannot be NaN, Infinity, or -Infinity
- Values cannot be undefined — use null instead
- Every opening bracket or brace must have a matching closing bracket or brace
- Backslashes in strings must be escaped: \\ not \
Error 1 — The Trailing Comma (Most Common)
This is statistically the most frequent JSON error. You add a new field to an object or array, then later remove the last item — and forget to remove the comma that was before it. Or you copy-paste from JavaScript code that allows trailing commas.
{
"name": "Vikas",
"city": "Jaipur",
"active": true,
}{
"name": "Vikas",
"city": "Jaipur",
"active": true
}The same rule applies to arrays. [1, 2, 3,] is invalid JSON. [1, 2, 3] is valid. Always check the element immediately before a closing brace or bracket — if it's a comma, remove it.
Error 2 — Single-Quoted Strings
JavaScript developers are especially prone to this one. In JavaScript, {'name': 'Priya'} is perfectly valid object syntax. In JSON, it is completely invalid. Every string in JSON — both keys and values — must use double quotes.
{'product': 'Chai',
'price': 120,
'origin': 'Darjeeling'}{"product": "Chai",
"price": 120,
"origin": "Darjeeling"}This commonly happens when developers manually type JSON, copy it from Python dictionaries (which use single quotes by default), or export it from tools that don't strictly adhere to the JSON spec. A quick find-and-replace can fix this if you're careful not to accidentally replace apostrophes inside string values.
Error 3 — Unquoted Keys
Another JavaScript-to-JSON confusion. In JavaScript, object keys don't need quotes: {name: "Ananya"} works fine. In JSON, every key must be a quoted string. This is one of the strictest differences between JSON and JavaScript object literal syntax.
{
id: 501,
name: "Meera",
active: true
}{
"id": 501,
"name": "Meera",
"active": true
}Error 4 — Comments Inside JSON
This trips up developers who come from languages where commenting config files is standard practice. JSON does not support comments. Not // line comments. Not /* block comments */. Nothing.
Error 5 — Missing Comma Between Elements
The opposite of a trailing comma — a missing comma between two consecutive key-value pairs or array elements. This happens most often when adding a new line manually or copy-pasting a block.
{
"name": "Sanjay"
"role": "admin"
"level": 3
}{
"name": "Sanjay",
"role": "admin",
"level": 3
}The parser error for this is typically something like Unexpected string or Expected comma or closing brace. The position reported is usually the start of the second key — not the end of the first value where the comma is missing.
Error 6 — Unescaped Special Characters in Strings
JSON strings support a specific set of escape sequences. Certain characters must be escaped with a backslash. The most common violations involve Windows file paths and newlines embedded in strings.
| Character | Must Be Written As | Common Mistake |
|---|---|---|
| Backslash \ | \\ | Windows paths: C:\Users\name |
| Double quote " | \" | Quotes inside string values |
| Newline | \n | Actual line break inside a string |
| Tab | \t | Actual tab character inside a string |
| Carriage return | \r | Windows line endings in string values |
{"path": "C:\Users\Rohit\docs"}{"path": "C:\\Users\\Rohit\\docs"}Error 7 — Invalid Number Formats
JSON has specific rules about numbers. These aren't always obvious:
- No leading zeros — 007 is invalid; use 7
- No NaN or Infinity — use null or an alternate representation
- No plus sign prefix — +42 is invalid; use 42
- Decimal point must have digits on both sides — .5 is invalid; use 0.5
- No currency symbols or commas in numbers — "₹1,499" as a number field should be 1499
Error 8 — Mismatched or Missing Brackets
In deeply nested JSON, it's easy to lose track of whether you've closed every object and array. The parser catches this immediately, but the reported error position can be misleading — it reports where it ran out of expected tokens, not necessarily where you forgot to close.
The best fix for bracket mismatch is to use a formatter first. Paste your JSON into a formatter — it will either show you the correctly indented structure (making the missing bracket visually obvious) or report the exact parse error with a useful position. A validator then confirms the fix.
Building a JSON Validation Workflow Into Your Development Process
The most effective developers don't debug JSON errors reactively — they prevent them proactively. Here's a workflow that catches JSON issues before they reach production:
- Never hand-write JSON for production. Always use your language's built-in serializer to generate JSON from native data structures. Hand-written JSON is where most errors originate.
- Validate API responses immediately. When integrating a new API, paste a sample response into a validator before writing any parsing code. Confirm the structure is what you expect.
- Add schema validation in your application. Beyond syntax validation, use a JSON Schema library to validate that incoming data has the right structure, types, and required fields.
- Lint JSON files in CI/CD. If your project uses JSON config files (package.json, tsconfig.json, etc.), add a JSON lint step to your CI pipeline to catch invalid edits before they merge.
- Use editor plugins. VS Code, JetBrains IDEs, and most modern editors have JSON validation built in or via plugins. Enable them — they catch errors as you type.
When Error Messages Are Confusing — How to Decode Them
The error messages from JSON.parse() are technically accurate but not always developer-friendly. Here's a translation guide:
| Error Message | What It Usually Means |
|---|---|
| Unexpected token , in JSON | Trailing comma — remove the comma before the closing bracket/brace |
| Unexpected token ' in JSON | Single-quoted string — change to double quotes |
| Unexpected token } in JSON | Missing comma between the previous value and this closing brace, or an extra closing brace |
| Unexpected string in JSON | Missing comma between two values — look at the line before the reported position |
| Unexpected end of JSON input | Missing closing bracket or brace — the JSON structure was never completed |
| Unexpected token u in JSON | The value undefined was used — replace with null |
| Unexpected token / in JSON | A comment was included — remove all // or /* */ comments |
Notice that error messages often point to the character after the problem, not the problem itself. If the error says position 47, look at positions 44–47 in your input — the actual mistake is usually one or two characters earlier.
JSON Validator in Multiple Languages
Developers across the world search for JSON validation tools in their own languages. Here's how "JSON Validator" is expressed globally:
✅ Validate Your JSON Right Now
Paste any JSON — from an API, a config file, or your own code — and get an instant verdict with a clear error message if something is wrong. No account needed to get started.
Open the 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.
Contact Us
Related Tools You May Like
🚀 Need Higher Limits?
- ✔ 390+ Tools
- ✔ AI Tools Included
- ✔ JS Tools 25 → 300 Uses/Day
- ✔ AI Tools 10 → 100 Uses/Day
- ✔ Higher Character Limits
- ✔ Exclusive Pro Features