JSON Syntax Error: How to Find and Fix It
A JSON syntax error occurs when your JSON doesn't follow the strict formatting rules of the JSON specification. Unlike some formats that are forgiving, JSON requires exact syntax—a single misplaced character will cause the entire document to fail parsing.
This guide will help you quickly identify what's wrong and fix it.
Finding the Error
Step 1: Use the JSON Validator
The fastest way to find a syntax error is to paste your JSON into the JSON Validator. It will:
- Highlight the exact line and character where the error occurs
- Explain what's wrong
- Show you the context around the error
Step 2: Read the Error Message
Error messages tell you what the parser expected vs. what it found:
| Error Message | Meaning |
|---|---|
| "Unexpected token" | Found a character that shouldn't be there |
| "Unexpected end of input" | JSON is incomplete (missing bracket or quote) |
| "Expected property name" | Missing quotes around a key |
| "Expected ':'" | Missing colon after a key |
| "Expected ',' or '}'" | Missing comma between properties |
Step 3: Check the Position
Most parsers report the position where the error was detected:
SyntaxError: Unexpected token } at position 45
Count to that position in your JSON, but remember: the actual error might be earlier. A missing comma on line 3 might not be detected until line 5.
The 8 Most Common Syntax Errors
1. Trailing Commas
The most common error. JSON doesn't allow commas after the last item:
{
"name": "Alice",
"age": 30, ← Error: trailing comma
}
Fix:
{
"name": "Alice",
"age": 30
}
In arrays too:
{
"colors": ["red", "blue", "green",] ← Error
}
2. Single Quotes
JSON requires double quotes. Single quotes are invalid:
{
'name': 'Alice' ← Error: single quotes
}
Fix:
{
"name": "Alice"
}
3. Unquoted Keys
Unlike JavaScript objects, JSON keys must be quoted:
{
name: "Alice", ← Error: unquoted key
age: 30 ← Error: unquoted key
}
Fix:
{
"name": "Alice",
"age": 30
}
4. Missing Commas
Forgetting commas between items:
{
"name": "Alice" ← Missing comma
"age": 30
}
Fix:
{
"name": "Alice",
"age": 30
}
5. Unescaped Characters
Special characters inside strings must be escaped:
{
"path": "C:\Users\Alice", ← Error: unescaped backslash
"quote": "She said "hello"" ← Error: unescaped quote
}
Fix:
{
"path": "C:\\Users\\Alice",
"quote": "She said \"hello\""
}
Characters that need escaping:
| Character | Escape Sequence |
|---|---|
" (quote) |
\" |
\ (backslash) |
\\ |
| Newline | \n |
| Tab | \t |
| Carriage return | \r |
Use our JSON Unescape tool to handle escape sequences.
6. Invalid Values
JSON only supports specific value types:
{
"callback": function() {}, ← Error: functions not allowed
"missing": undefined, ← Error: use null instead
"notANumber": NaN, ← Error: use null or string
"infinite": Infinity ← Error: use null or string
}
Valid JSON values:
- Strings:
"hello" - Numbers:
42,3.14,-17,1.2e10 - Booleans:
true,false - Null:
null - Arrays:
[1, 2, 3] - Objects:
{"key": "value"}
7. Wrong Boolean/Null Case
Booleans and null must be lowercase:
{
"active": True, ← Error: should be true
"verified": FALSE, ← Error: should be false
"data": NULL ← Error: should be null
}
Fix:
{
"active": true,
"verified": false,
"data": null
}
8. Comments
Standard JSON doesn't support comments:
{
// This is a comment ← Error
"name": "Alice",
/* Another comment */ ← Error
"age": 30
}
Fix: Remove all comments. See our JSON Comments guide for alternatives.
Language-Specific Error Messages
JavaScript
try {
JSON.parse('{"name": "Alice",}');
} catch (e) {
console.log(e.message);
// "Unexpected token } in JSON at position 17"
}
Python
import json
try:
json.loads('{"name": "Alice",}')
except json.JSONDecodeError as e:
print(f"Error at line {e.lineno}, column {e.colno}: {e.msg}")
# "Error at line 1, column 18: Expecting property name enclosed in double quotes"
Java (Jackson)
try {
new ObjectMapper().readTree("{\"name\": \"Alice\",}");
} catch (JsonProcessingException e) {
System.out.println(e.getOriginalMessage());
// "Unexpected character ('}' (code 125))"
}
PHP
$result = json_decode('{"name": "Alice",}');
if (json_last_error() !== JSON_ERROR_NONE) {
echo json_last_error_msg();
// "Syntax error"
}
Debugging Strategies
1. Binary Search
For large JSON files, use binary search to find the error:
- Delete the second half of the file
- If it parses, the error is in the deleted half
- If it doesn't parse, the error is in the first half
- Repeat until you find it
2. Format First
Unformatted JSON is hard to debug:
{"users":[{"name":"Alice","age":30},{"name":"Bob","age":25}],"total":2}
Format it first using the JSON Validator to see the structure:
{
"users": [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25}
],
"total": 2
}
3. Check Bracket Matching
Missing or extra brackets are common. Count them:
- Every
{needs a} - Every
[needs a] - Every
"needs a closing"
Most code editors highlight matching brackets.
4. Look Before the Error
The parser reports where it detected the problem, not where the problem is:
{
"name": "Alice"
"age": 30 ← Error reported here
}
The error is the missing comma on line 2, but it's detected on line 3.
5. Validate Incrementally
When building JSON programmatically, validate after each addition:
let data = {};
console.log(JSON.stringify(data)); // Valid
data.name = "Alice";
console.log(JSON.stringify(data)); // Valid
data.items = getItems(); // Problem might be here
console.log(JSON.stringify(data)); // Check if still valid
Prevention
Use JSON Libraries
Never build JSON strings manually:
// Bad - prone to errors
const json = '{"name": "' + name + '", "age": ' + age + '}';
// Good - handles escaping automatically
const json = JSON.stringify({ name, age });
Use a Linter
Configure your editor to validate JSON files:
- VS Code: Built-in JSON validation
- Sublime Text: JSONLint package
- Vim: ALE or Syntastic
Use Schema Validation
Define the expected structure with JSON Schema:
{
"type": "object",
"required": ["name", "age"],
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0}
}
}
Test with Real Data
Always test your JSON generation with edge cases:
- Empty strings
- Special characters (quotes, backslashes, unicode)
- Very large numbers
- Deeply nested objects
Quick Reference
| Problem | Solution |
|---|---|
| Trailing comma | Remove comma after last item |
| Single quotes | Replace with double quotes |
| Unquoted key | Add double quotes around key |
| Missing comma | Add comma between items |
| Unescaped backslash | Use \\ |
| Unescaped quote | Use \" |
undefined value |
Use null |
NaN or Infinity |
Use null or string |
| Comments | Remove them |
| Wrong case boolean | Use lowercase true/false |
Related Tools & Resources
Tools
- JSON Validator — Find and fix syntax errors instantly
- JSON Unescape — Handle escape sequences
- JSON Stringify — Properly escape strings
- JSON Minify — Compact valid JSON
Learn More
- JSON Parse Error Guide — Detailed error handling
- Common JSON Mistakes — Complete error list
- JSON Comments Guide — Why comments fail
- Fix Unexpected End of JSON — Incomplete JSON errors
Related Articles
Understanding the Benefits of Using a JSON Beautifier
Learn why formatting and beautifying JSON improves code readability, debugging, and collaboration.
Common JSON Mistakes and How to Avoid Them
The 20 most common JSON syntax errors developers make, organized by category with examples and fixes.
How to Fix 'Unexpected End of JSON Input' Error
Learn what causes the 'Unexpected end of JSON input' error and how to fix it with practical solutions for JavaScript, Python, and other languages.
How to Open JSON Files
A complete guide to opening and viewing JSON files on any operating system using various methods and tools.