JSON Not Valid: How to Find the Exact Error
JSON validation errors are vague by default. Here's how to pinpoint and fix the broken line quickly.
You paste a JSON blob and get: SyntaxError: Unexpected token or JSON parse error at position 2847. The file is 3,000 characters long. Where's the actual problem?
Here's how to find it fast.
The Fastest Method: Use a JSON Formatter with Validation
The JSON formatter highlights the exact error location not just a character position, but the specific line and what went wrong (trailing comma, missing quote, unexpected character).
Paste your JSON. If it's invalid, you'll see the error highlighted in red with a description. Most problems are immediately visible.
Common JSON Errors and How to Spot Them
1. Trailing Comma
The most common JSON error, especially for people coming from JavaScript (where trailing commas are allowed).
Invalid:
{
"name": "Alice",
"age": 30,
}
Valid:
{
"name": "Alice",
"age": 30
}
The comma after 30 is illegal. JSON parsers fail here.
How to spot: Look for commas before } or ]. Any comma as the last element in an object or array is invalid.
2. Single Quotes Instead of Double Quotes
JavaScript allows single or double quotes. JSON requires double quotes for both keys and string values.
Invalid:
{'name': 'Alice'}
Valid:
{"name": "Alice"}
How to spot: Search for ' in your JSON. Any occurrence is an error unless it's inside a double-quoted string.
3. Unquoted Keys
JavaScript object literals allow unquoted keys. JSON doesn't.
Invalid:
{name: "Alice"}
Valid:
{"name": "Alice"}
How to spot: Keys not surrounded by double quotes.
4. Comments
JSON doesn't support comments. Adding them breaks the parser.
Invalid:
{
"name": "Alice", // user name
"age": 30
}
Valid: Remove the comment. If you need comments in config files, use JSONC (JSON with Comments) or YAML instead.
5. Unterminated String
A string that opens with " but doesn't close often caused by copy-paste truncation.
Invalid:
{
"message": "Hello, world
}
How to spot: Your editor's string syntax highlighting shows the whole rest of the file highlighted as a string. Or look for newlines inside quoted values (only allowed if escaped as \n).
6. Incorrect Escape Sequences
JSON supports specific escape sequences: \", \\, \/, \b, \f, \n, \r, \t, \uXXXX. Other backslash combinations are invalid.
Invalid:
{"path": "C:\Users\alice"}
Valid:
{"path": "C:\\Users\\alice"}
A single backslash followed by U, a, or other non-escape characters is a syntax error.
7. Non-String Keys
JSON object keys must be strings. Numbers, booleans, and null are not valid keys.
Invalid:
{1: "one", true: "yes"}
Valid:
{"1": "one", "true": "yes"}
8. Undefined, NaN, Infinity
These are JavaScript-specific values. JSON has no equivalent.
Invalid:
{"value": undefined}
{"ratio": NaN}
{"score": Infinity}
Valid: Use null for undefined/missing values. Replace NaN and Infinity with null or an actual number.
9. Trailing Data After the Root
JSON must have exactly one root value. Multiple values without an enclosing array or object is invalid.
Invalid:
{"a": 1}
{"b": 2}
Valid:
[{"a": 1}, {"b": 2}]
10. Wrong Number Format
JSON numbers must not have leading zeros (except 0.5) and must not have trailing decimals (1.).
Invalid:
{"count": 07}
{"price": 1.}
Valid:
{"count": 7}
{"price": 1.0}
Locating the Error by Position
When you get an error like position 2847, count isn't helpful by hand. Use this approach:
In the browser: Open DevTools console, paste:
JSON.parse(yourJsonString)
The error message usually includes a line number in the stack trace.
In Python:
import json
try:
data = json.loads(your_json_string)
except json.JSONDecodeError as e:
print(f"Line {e.lineno}, column {e.colno}: {e.msg}")
In the JSON formatter: Paste the JSON. The error is highlighted with line and character position.
Fixing JSON from External Sources
If you received the JSON from an API, database export, or someone else's system:
- Paste into the JSON formatter first it will immediately show if it's valid
- If invalid, look at the highlighted error
- Fix the source system if possible, not just the symptom
If you're regularly receiving invalid JSON from an API and can't fix the source, use a lenient parser (available in Python with demjson3 or json5 libraries) for preprocessing.
When the JSON is "Valid" But Your App Still Fails
Validation errors and schema errors are different.
A JSON can be syntactically valid (parseable) but structurally wrong for your application (missing required fields, wrong types, unexpected nesting).
For schema validation, use tools like:
- JSON Schema Validator paste a JSON Schema and validate against it
ajvin Node.js for programmatic validation
The JSON formatter handles syntax validation (is this parseable JSON?). Schema validation is a separate layer.
Paste your JSON into the JSON formatter it shows the exact error line and provides a human-readable explanation. Most JSON problems are fixed in under a minute once you can see where the problem is.