Quick Answer
Most JSON parse errors come from trailing commas, missing commas, unquoted keys, single quotes, broken brackets, or comments in strict JSON. Use the JSON Validator first to find the exact problem, then re-run the payload through the JSON Formatter once it is valid.
The Most Common JSON Parse Errors
| Error type | Broken example | Fixed example |
|---|---|---|
| Trailing comma | {"a":1,} |
{"a":1} |
| Missing comma | {"a":1 "b":2} |
{"a":1,"b":2} |
| Unquoted key | {name:"Alice"} |
{"name":"Alice"} |
| Single quotes | {'name':'Alice'} |
{"name":"Alice"} |
| Broken bracket or brace | {"a":[1,2} |
{"a":[1,2]} |
| Comment inside JSON | {"a":1 // note} |
Remove comments from strict JSON |
| Unescaped control character | {"msg":"line1\nline2"} (literal newline) |
{"msg":"line1\\nline2"} |
| Hexadecimal numbers | {"color":0xFF} |
{"color":255} |
Error-by-Error Walkthrough
Trailing Comma
A comma after the last property or array element is valid in JavaScript but illegal in JSON. This is the single most common parse error because developers copy objects from JS source code directly into JSON files.
// Breaks: trailing comma after "editor"
{"roles": ["admin", "editor",]}
// Fixed
{"roles": ["admin", "editor"]}
Missing Comma
When a comma is missing between two key-value pairs or array elements, the parser tries to interpret two tokens as one and fails. The error position often points to the second key, not the gap between them.
// Breaks: no comma between "name" and "age"
{"name": "Alice" "age": 30}
// Fixed
{"name": "Alice", "age": 30}
Unquoted Keys and Single Quotes
JSON requires double quotes around all keys and string values. JavaScript allows unquoted keys and single quotes, but JSON does not. This mismatch causes errors when developers paste JS object literals into JSON fields.
// Both break in strict JSON
{name: 'Alice'}
// Fixed
{"name": "Alice"}
Unescaped Special Characters
JSON strings cannot contain literal newlines, tabs, or other control characters. They must use escape sequences: \n, \t, \\, \". A literal newline byte inside a quoted string will cause a parse failure.
Mismatched Brackets and Braces
Every [ needs a matching ], and every { needs a matching }. When brackets are mismatched, the error message often appears far from the actual mistake because the parser keeps reading, looking for the closing delimiter.
Language-Specific Error Messages
Different languages report JSON errors differently. Knowing your language's error format helps you locate the problem faster.
| Language | Parse function | Typical error message |
|---|---|---|
| JavaScript | JSON.parse() |
SyntaxError: Unexpected token } in JSON at position 12 |
| Python | json.loads() |
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 3 column 1 |
| Java | ObjectMapper.readValue() |
JsonParseException: Unexpected character ('}') at offset 12 |
| Go | json.Unmarshal() |
invalid character '}' looking for beginning of object key string |
| Ruby | JSON.parse() |
JSON::ParserError: unexpected token at '}' |
| PHP | json_decode() |
Returns null silently; check json_last_error_msg() |
Note that PHP's json_decode() returns null on failure without throwing an exception by default. Always call json_last_error() after parsing in PHP to detect failures.
Debugging Workflow
- Isolate the payload. Copy the raw JSON string into a standalone file or the JSON Validator. Do not debug inside your application code.
- Find the first error. Parsers report the position of the first failure. Ignore all downstream errors until you fix this one.
- Check the character at the reported position. Look at the character and the 5-10 characters before it. The actual mistake is usually just before the reported position.
- Look for structural patterns. Count opening and closing braces and brackets. Use a text editor with bracket matching to find mismatches.
- Validate incrementally. If the payload is large, validate subsections by extracting nested objects and parsing them individually.
- Format after fixing. Once the payload parses cleanly, run it through the JSON Formatter to confirm the structure is what you expect.
How to Fix Parse Errors Fast
- Paste the payload into the JSON Validator.
- Look for the first reported line and character position.
- Fix the syntax issue nearest that point.
- Validate again until the payload parses cleanly.
- Format the valid result with the JSON Formatter for readability.
Why the First Error Matters Most
One broken comma or bracket often causes many later parse errors. Fix the first real syntax problem first. Do not chase every downstream error message until the parser can get past the earliest failure.
JSON5 and JSONC: When Strict JSON Is Not Enough
Strict JSON does not allow comments, trailing commas, or single-quoted strings. Two extensions address this for config files and development workflows:
- JSON5: Allows single-quoted strings, unquoted keys, trailing commas, comments (
//and/* */), hexadecimal numbers, and multiline strings. Used in some build tools and config files. - JSONC (JSON with Comments): The subset used by VS Code configuration files. Allows
//and/* */comments and trailing commas but otherwise follows JSON syntax.
If your parse errors come from comments or trailing commas in a config file, check whether the consuming tool supports JSON5 or JSONC. If it does, the syntax is valid for that context. If you need strict JSON for an API, strip comments and trailing commas before sending.
API Response Debugging Tips
When an API returns JSON that fails to parse, the problem may not be in the JSON itself:
- Check the Content-Type header. If the response is HTML (a 404 page or error page) rather than
application/json, your parser will fail on the HTML tags, not on invalid JSON. - Look for BOM characters. Some servers prepend a UTF-8 BOM (
\uFEFF) to the response. This invisible character before the opening{causes parse failures. - Check for truncation. If the response was cut off mid-stream (timeout, network error), the JSON will be incomplete and unparseable. Look for missing closing brackets.
- Inspect for JSONP wrapping. Some APIs wrap JSON in a callback function:
callback({"data":1}). Strip the function wrapper before parsing.
Automated JSON Repair
Some tools attempt to automatically fix broken JSON by adding missing commas, removing trailing commas, or converting single quotes to double quotes. Use these with caution:
- Automated repair can misinterpret your intended structure when multiple fixes are possible.
- A missing comma could mean a missing key-value pair, not just a missing separator. The tool cannot know which you intended.
- For production pipelines, always fix the JSON at the source rather than relying on downstream repair.
- Repair tools are useful for one-off debugging of pasted data, not for automated pipelines where silent corruption is dangerous.
When Not to Use a Formatter First
If the payload is invalid, a formatter cannot safely pretty-print it. Formatting is for readable valid JSON. Validation is for broken JSON. Keep those jobs separate to avoid confusion.
FAQ
Why does valid JavaScript object syntax still fail as JSON?
JSON is stricter than JavaScript object syntax. It requires double-quoted keys and strings, and it does not allow comments, trailing commas, or undefined values. JSON is a data interchange format derived from JavaScript, but it is not JavaScript.
Can a validator repair JSON automatically?
No. A validator tells you where the problem is. You still need to fix the syntax manually or regenerate the payload correctly. Automated repair tools exist but can misinterpret your intended structure.
Should I minify broken JSON?
No. Minify only after the payload is valid and ready for production use. Minifying broken JSON will either fail or produce an unparseable compressed string.
Why does my JSON parse error point to the wrong line?
Parsers report where they first encounter an unexpected token, which is often after the actual mistake. A missing comma on line 5 might cause the parser to fail on line 6 when it encounters the next key without a preceding separator.
How do I handle JSON with comments in a strict parser?
Strip the comments before parsing. You can use a regex to remove // line comments and /* */ block comments, or use a library that supports JSONC or JSON5 parsing. Never send commented JSON to an API that expects strict JSON.
Why does PHP json_decode return null instead of an error?
PHP's json_decode() returns null on failure by default. Always check json_last_error() after calling it. In PHP 7.3+, you can pass JSON_THROW_ON_ERROR as a flag to get exceptions instead.
Can a BOM character break JSON parsing?
Yes. A UTF-8 BOM (\uFEFF) at the start of a file or response is invisible but causes parsers to fail because the first character is not { or [. Trim BOM bytes before parsing.
Related Tools
- JSON Validator for exact syntax errors
- JSON Formatter for readable output after repair
- JSON Minifier for compact output once debugging is complete
- CSV to JSON Converter when parse errors come from conversion output
Related Guides
- How to Format JSON Correctly for the full formatting workflow
- Data Format Conversion when errors originate from CSV-to-JSON pipelines