JSON Validation Errors: Common Mistakes & How to Fix Them
Learn how to identify and fix the most common JSON validation errors. This comprehensive guide includes examples, error messages, and step-by-step solutions.
Quick Fix Guide
If you're here to quickly fix a JSON error, jump to the specific error type below or use ourJSON Validator tool for instant error detection.
Understanding JSON Validation
JSON validation ensures that your data follows the correct JSON syntax rules. Invalid JSON can break applications, cause API failures, and lead to data corruption. Understanding common validation errors and their fixes is essential for anyone working with JSON data.
Most Common JSON Validation Errors
1. Syntax Error: Unexpected Token
This is the most common JSON error, usually caused by incorrect punctuation or formatting.
Single Quotes Instead of Double Quotes
❌ Error:
{
'name': 'John Doe',
'age': 30
}
Error: Unexpected token ' in JSON at position 2
✅ Fix:
{
"name": "John Doe",
"age": 30
}
Solution: Always use double quotes for both keys and string values in JSON.
Missing Quotes Around Keys
❌ Error:
{
name: "John Doe",
age: 30
}
Error: Unexpected token n in JSON at position 2
✅ Fix:
{
"name": "John Doe",
"age": 30
}
2. Trailing Comma Errors
JSON doesn't allow trailing commas, unlike JavaScript objects.
❌ Error:
{
"name": "John Doe",
"age": 30,
}
Error: Unexpected token } in JSON at position 25
✅ Fix:
{
"name": "John Doe",
"age": 30
}
Array Trailing Comma:
❌ Error:
{
"hobbies": ["reading", "coding", "swimming",]
}
Error: Unexpected token ] in JSON at position 43
✅ Fix:
{
"hobbies": ["reading", "coding", "swimming"]
}
3. Missing or Extra Commas
Missing Comma Between Properties
❌ Error:
{
"name": "John Doe"
"age": 30
}
Error: Unexpected string in JSON at position 18
✅ Fix:
{
"name": "John Doe",
"age": 30
}
Missing Comma Between Array Elements
❌ Error:
{
"colors": ["red" "blue" "green"]
}
Error: Unexpected string in JSON at position 18
✅ Fix:
{
"colors": ["red", "blue", "green"]
}
4. Incorrect Data Types and Values
Undefined Values
❌ Error:
{
"name": "John Doe",
"salary": undefined
}
Error: Unexpected token u in JSON at position 25
✅ Fix:
{
"name": "John Doe",
"salary": null
}
Unquoted String Values
❌ Error:
{
"name": John Doe,
"age": 30
}
Error: Unexpected token J in JSON at position 10
✅ Fix:
{
"name": "John Doe",
"age": 30
}
5. Comments in JSON (Not Allowed)
❌ Error:
{
// User information
"name": "John Doe",
"age": 30
}
Error: Unexpected token / in JSON at position 2
✅ Fix:
{
"name": "John Doe",
"age": 30
}
Alternative for documentation: Use a special key for comments or use a separate documentation file.
6. Bracket and Brace Mismatches
Missing Closing Brace
❌ Error:
{
"user": {
"name": "John Doe",
"age": 30
"status": "active"
}
Error: Unexpected string in JSON at position 42
✅ Fix:
{
"user": {
"name": "John Doe",
"age": 30
},
"status": "active"
}
Wrong Bracket Type
❌ Error:
{
"numbers": (1, 2, 3, 4, 5)
}
Error: Unexpected token ( in JSON at position 13
✅ Fix:
{
"numbers": [1, 2, 3, 4, 5]
}
7. Escape Character Issues
Unescaped Quotes in Strings
❌ Error:
{
"quote": "She said "Hello" to me"
}
Error: Unexpected token H in JSON at position 18
✅ Fix:
{
"quote": "She said "Hello" to me"
}
Unescaped Backslashes
❌ Error:
{
"path": "C:UsersJohnDocuments"
}
Error: Unexpected token U in JSON at position 12
✅ Fix:
{
"path": "C:\Users\John\Documents"
}
Advanced JSON Validation Issues
1. Duplicate Keys
While technically valid JSON, duplicate keys can cause unexpected behavior.
⚠️ Problematic:
{
"name": "John Doe",
"age": 30,
"name": "Jane Smith"
}
// Last value wins: name will be "Jane Smith"
✅ Better:
{
"firstName": "John",
"lastName": "Doe",
"age": 30
}
2. Encoding Issues
❌ Error with special characters:
{
"name": "José",
"city": "São Paulo"
}
// May cause encoding errors if not properly handled
✅ Fix with Unicode escapes:
{
"name": "Jos\u00e9",
"city": "S\u00e3o Paulo"
}
JSON Validation Tools and Techniques
Online Validators
- AllToolsHQ JSON Formatter: Instant validation with detailed error messages
- JSONLint: Popular online JSON validator
- JSON Validator by Code Beautify: Comprehensive validation tool
Command Line Validation
Using Python
# Validate JSON file
python -m json.tool input.json
# Validate JSON string
echo '{"name": "John"}' | python -m json.tool
Using jq
# Validate and pretty-print
jq . input.json
# Check if valid (silent)
jq empty input.json
Programmatic Validation
JavaScript
function validateJSON(jsonString) {
try {
JSON.parse(jsonString);
return { valid: true, error: null };
} catch (error) {
return { valid: false, error: error.message };
}
}
const result = validateJSON('{"name": "John"}');
console.log(result.valid); // true
Python
import json
def validate_json(json_string):
try:
json.loads(json_string)
return True, None
except json.JSONDecodeError as e:
return False, str(e)
valid, error = validate_json('{"name": "John"}')
print(f"Valid: {valid}") # Valid: True
Best Practices for Avoiding JSON Errors
1. Use Proper Development Tools
- Configure your code editor with JSON syntax highlighting
- Use linters like ESLint with JSON rules
- Install JSON formatter extensions
- Enable auto-formatting on save
2. Validate During Development
- Always validate JSON before committing code
- Use automated testing for JSON APIs
- Implement JSON schema validation in CI/CD pipelines
- Test with edge cases and malformed data
3. Error Handling in Applications
- Always wrap JSON parsing in try-catch blocks
- Provide meaningful error messages to users
- Log detailed errors for debugging
- Implement fallback behavior for invalid JSON
Common JSON Error Messages Explained
Error Message Dictionary
- "Unexpected token": Usually indicates incorrect syntax or punctuation
- "Unexpected end of JSON input": JSON is incomplete or truncated
- "Expected property name or '}'": Missing or malformed object property
- "Unexpected string": Often missing comma between properties
- "Unexpected number": Number in wrong position or format
Conclusion
JSON validation errors are common but easily preventable with proper understanding and tools. By following the examples and best practices in this guide, you can avoid most validation issues and quickly fix any that occur.
Remember to always validate your JSON during development and implement proper error handling in your applications. Use tools like our JSON Formatter to catch errors early and maintain clean, valid JSON data.
Validate Your JSON Now
Use our free JSON validator to check your JSON for errors and get instant feedback with detailed error messages.
Validate JSON