If you've worked with JSON, you've likely encountered the dreaded "Unexpected token" or "Invalid JSON" error. These errors can be frustrating, especially when the problem isn't immediately obvious. Let's look at the most common JSON errors and how to fix them.
1. Trailing Commas
One of the most common errors. JSON does not allow a comma after the last element in an array or object, unlike JavaScript.
// Invalid - trailing comma in array
{
"colors": ["red", "green", "blue",]
}
// Invalid - trailing comma in object
{
"name": "John",
"age": 30,
}
// Valid - no trailing commas
{
"colors": ["red", "green", "blue"]
}
{
"name": "John",
"age": 30
}
2. Single Quotes Instead of Double Quotes
JSON requires double quotes for strings. Single quotes are not valid, even though they work in JavaScript.
// Invalid - single quotes
{
'name': 'John',
'email': '[email protected]'
}
// Valid - double quotes
{
"name": "John",
"email": "[email protected]"
}
3. Unquoted Property Names
In JSON, all property names (keys) must be enclosed in double quotes. JavaScript allows unquoted keys, but JSON does not.
// Invalid - unquoted keys
{
name: "John",
age: 30
}
// Valid - quoted keys
{
"name": "John",
"age": 30
}
4. Comments
JSON does not support comments. If you need to include comments, consider using a dedicated field or switching to a format like JSON5 or JSONC.
// Invalid - comments not allowed
{
// User information
"name": "John",
/* This is the user's age */
"age": 30
}
// Valid - use a description field instead
{
"_comment": "User information",
"name": "John",
"age": 30
}
5. Missing or Extra Brackets/Braces
Every opening bracket or brace must have a corresponding closing one. This error is common when manually editing JSON.
// Invalid - missing closing brace
{
"user": {
"name": "John"
}
// Invalid - extra bracket
{
"items": ["a", "b", "c"]]
}
6. Incorrect Boolean or Null Values
JSON boolean and null values must be lowercase. Other forms like True, TRUE,
False, FALSE, None, or NULL are invalid.
// Invalid - wrong casing
{
"isActive": True,
"isAdmin": FALSE,
"middleName": None
}
// Valid - lowercase
{
"isActive": true,
"isAdmin": false,
"middleName": null
}
7. Missing Commas Between Elements
Each element in an array or property in an object must be separated by a comma.
// Invalid - missing comma
{
"name": "John"
"age": 30
}
// Invalid - missing comma in array
["apple" "banana" "cherry"]
8. Invalid Escape Sequences
Special characters in strings must be properly escaped. Common escape sequences
include \" for quotes, \\ for backslash, and \n for newline.
// Invalid - unescaped quotes
{
"message": "He said "Hello""
}
// Valid - properly escaped
{
"message": "He said \"Hello\""
}
9. Leading Zeros in Numbers
JSON does not allow leading zeros in numbers (except for decimal numbers less than 1).
// Invalid - leading zeros
{
"code": 007,
"zipCode": 01234
}
// Valid - no leading zeros (use strings if needed)
{
"code": 7,
"zipCode": "01234"
}
10. Undefined Values
JavaScript's undefined is not valid in JSON. Use null instead, or omit the property.
// Invalid
{
"value": undefined
}
// Valid
{
"value": null
}
Debugging Tips
- Use our JSON Formatter to validate and find errors
- Check the error message for line numbers and character positions
- Look for the common issues listed above
- Use our JSON Fixer to automatically repair simple errors
- When copying from code, ensure you're not including JavaScript-specific syntax
Conclusion
Most JSON errors are simple syntax mistakes that are easy to fix once you know what to look for. Bookmark this page for quick reference, and use our free tools to validate and repair your JSON data.