10 Common JSON Errors and How to Fix Them
You would think a format this simple would be hard to mess up. But JSON is strict in ways that catch people off guard, especially if you are coming from JavaScript where the syntax is more forgiving. Here are the ten mistakes I see most often and what to do about each one.
1. Trailing Commas
This is the number one offender. In JavaScript, you can leave a comma after the last item in an array or object and nobody complains. JSON will reject it immediately.
// Wrong
{ "fruits": ["apple", "banana",] }
// Right
{ "fruits": ["apple", "banana"] }Fix: Remove the comma after the last element. Most code editors have a setting to flag trailing commas in JSON files.
2. Single Quotes Instead of Double Quotes
JSON requires double quotes for both keys and string values. Single quotes are valid in JavaScript and Python, but JSON parsers will reject them every time.
// Wrong
{ 'name': 'Alice' }
// Right
{ "name": "Alice" }Fix: Use find-and-replace to swap single quotes for double quotes. Watch out for apostrophes inside strings — those need escaping.
3. Unquoted Keys
JavaScript lets you write object keys without quotes. JSON does not. Every key must be a double-quoted string.
// Wrong
{ name: "Alice" }
// Right
{ "name": "Alice" }Fix: Wrap every key in double quotes. If you are generating JSON from code, use your language's built-in serializer rather than building strings manually.
4. Missing or Extra Brackets
When you are working with deeply nested JSON, it is easy to lose track of opening and closing braces or brackets. One missing bracket and the whole document is invalid.
Fix: Use a formatter or validator that highlights matching brackets. Most editors also have bracket-matching features built in.
5. Comments in JSON
This catches a lot of people. JSON does not allow comments — not //, not /* */, not #. If you add one, the parser will fail.
Fix: Remove all comments before parsing. If you need comments in config files, consider using JSONC (JSON with Comments) or JSON5, which both support them.
6. Using undefined or NaN
These are JavaScript values that have no equivalent in JSON. If your code serializes an object that contains undefined, most serializers will silently drop the field. If they include it literally, the JSON is broken.
Fix: Use null instead of undefined. For NaN, decide on a sentinel value (like null or a specific number) and document the convention.
7. Leading Zeros in Numbers
Writing 007 might look fine, but JSON parsers treat leading zeros as an error. The only exception is 0 itself or decimals like 0.5.
Fix: Remove the leading zeros. If you need the formatting (like ZIP codes), store the value as a string instead.
8. Unescaped Special Characters in Strings
Certain characters inside JSON strings must be escaped with a backslash: double quotes, backslashes themselves, newlines, tabs, and a few control characters. Raw newlines inside a string will break the parser.
// Wrong
{ "message": "Line 1
Line 2" }
// Right
{ "message": "Line 1\nLine 2" }Fix: Use proper escape sequences. Your language's JSON serializer handles this automatically — the issue usually comes from manually editing JSON files.
9. Wrong Data Type for a Value
This is less about syntax and more about semantics, but it causes real problems. Sending a number as a string (or vice versa) can break API consumers that validate against a strict schema.
{ "port": "8080" } // string
{ "port": 8080 } // numberFix: Check the API documentation or JSON Schema for expected types. Be deliberate about whether something should be a string, number, or boolean.
10. BOM or Invisible Characters
Sometimes a JSON file parses fine in one tool but fails in another. The culprit is often a Byte Order Mark (BOM) at the beginning of the file, or invisible Unicode characters (like zero-width spaces) that crept in from a copy-paste.
Fix: Open the file in a hex editor or use a tool that shows invisible characters. Save your JSON files as UTF-8 without BOM. If you are pasting from a web page or Word document, paste into a plain text editor first to strip formatting.
Quick Prevention Checklist
- Always use your language's JSON serializer instead of building JSON strings manually.
- Validate JSON before sending it to an API or saving it to a file.
- Use an editor with JSON syntax highlighting and bracket matching.
- Set up a linter for JSON files in your project.
- When debugging, paste the JSON into a formatter to see the error location.