Common JSON Validation Errors and How to Fix Them
A comprehensive guide to the most common JSON syntax errors and step-by-step solutions to resolve them quickly.
JSON validation errors can be frustrating, especially when you're trying to debug API responses or configuration files. The good news is that most JSON errors fall into predictable categories. This guide covers the most common validation errors you'll encounter and provides clear solutions for each one.
1. Missing or Extra Commas
Comma-related errors are among the most frequent JSON validation issues. JSON requires commas between elements but prohibits trailing commas.
❌ Error: Missing comma between elements
{ "name": "John Doe" "age": 30, "city": "New York" }
Error: Expected ',' or '}' after property value
✅ Fix: Add missing comma
{ "name": "John Doe", "age": 30, "city": "New York" }
❌ Error: Trailing comma
{ "name": "John Doe", "age": 30, "city": "New York", }
Error: Trailing comma in object
✅ Fix: Remove trailing comma
{ "name": "John Doe", "age": 30, "city": "New York" }
2. Unmatched Brackets and Braces
Every opening bracket [
must have a closing bracket ]
, and every opening brace {
must have a closing brace }
.
❌ Error: Missing closing brace
{ "users": [ { "id": 1, "name": "John" }, { "id": 2, "name": "Jane" } ] // Missing closing brace for the main object
Error: Unexpected end of JSON input
✅ Fix: Add missing closing brace
{ "users": [ { "id": 1, "name": "John" }, { "id": 2, "name": "Jane" } ] }
💡 Pro Tip
Use proper indentation to visually track opening and closing brackets. Most code editors can highlight matching brackets when you hover over them.
3. Incorrect String Quotes
JSON strings must use double quotes ("
). Single quotes ('
) are not valid in JSON.
❌ Error: Single quotes used
{ 'name': 'John Doe', 'age': 30, 'isActive': true }
Error: Unexpected token ' in JSON
✅ Fix: Use double quotes
{ "name": "John Doe", "age": 30, "isActive": true }
❌ Error: Unescaped quotes in string
{ "message": "He said "Hello" to me" }
Error: Unexpected token H in JSON
✅ Fix: Escape internal quotes
{ "message": "He said \"Hello\" to me" }
4. Invalid Property Names
All property names (keys) in JSON must be strings enclosed in double quotes.
❌ Error: Unquoted property names
{ name: "John Doe", age: 30, email: "john@example.com" }
Error: Unexpected token n in JSON
✅ Fix: Quote all property names
{ "name": "John Doe", "age": 30, "email": "john@example.com" }
5. Invalid Value Types
JSON only supports specific value types: strings, numbers, booleans, null, objects, and arrays. JavaScript-specific values like undefined
or functions are not valid.
❌ Error: Using undefined
{ "name": "John Doe", "middleName": undefined, "age": 30 }
Error: Unexpected token u in JSON
✅ Fix: Use null or omit the property
{ "name": "John Doe", "middleName": null, "age": 30 }
❌ Error: Including functions
{ "name": "John Doe", "greet": function() { return "Hello!"; } }
Error: Unexpected token f in JSON
✅ Fix: Remove functions or represent as strings
{ "name": "John Doe", "greetingMessage": "Hello!" }
6. Number Format Errors
JSON has specific rules for number formatting. Leading zeros, trailing decimals, and certain notations are not allowed.
❌ Error: Leading zeros
{ "price": 029.99, "quantity": 007 }
✅ Fix: Remove leading zeros
{ "price": 29.99, "quantity": 7 }
❌ Error: Trailing decimal point
{ "price": 29., "discount": 5. }
✅ Fix: Complete the decimal or use integers
{ "price": 29.0, "discount": 5 }
7. Control Characters and Encoding Issues
Invisible control characters can cause validation errors that are hard to spot visually.
⚠️ Common control character issues:
- BOM (Byte Order Mark): Invisible character at the beginning of files
- Non-breaking spaces: Look like regular spaces but aren't
- Tab vs. Space mixing: Can cause parsing issues
- Line ending inconsistencies: Different systems use different line endings
Solution: Use a proper text editor that can show invisible characters, or copy-paste your JSON into TidyJSON to identify and fix these issues.
8. Debugging Tips and Tools
1. Use Line Numbers
Most JSON validation tools provide line numbers where errors occur. Use these to quickly locate problems in large files.
2. Validate Incrementally
When dealing with large JSON files, comment out sections and validate smaller parts to isolate the problematic area.
3. Use Formatting Tools
Properly formatted JSON makes errors easier to spot. Use tools like TidyJSON to format your JSON and highlight syntax issues.
Try TidyJSON →4. Browser Developer Tools
Most browser developer consoles can parse JSON. Try JSON.parse(yourString)
in the console for quick validation.
Quick Reference: Error Messages
Conclusion
Most JSON validation errors are straightforward to fix once you know what to look for. The key is understanding JSON's strict syntax rules and using the right tools to identify and resolve issues quickly.
Remember that JSON is less forgiving than JavaScript object literals – it requires double quotes, forbids trailing commas, and doesn't support JavaScript-specific values. When in doubt, use a JSON validator to catch errors before they cause problems in your applications.
💡 Final Tip
Keep TidyJSON bookmarked for quick JSON validation and formatting. It catches syntax errors in real-time and helps you maintain clean, valid JSON in all your projects.