How to Fix Invalid JSON
JSON is strict on purpose. Almost every parse error comes from one of six mistakes, and each has a one-line fix. Knowing the six turns a frustrating debugging session into a two-minute correction.
The six errors
| Error | Looks like | Fix |
|---|---|---|
| Trailing comma | {"a":1,} | Remove the final comma |
| Single quotes | {'a':1} | Use double quotes |
| Unquoted key | {a:1} | Quote the key |
| Comment | // note | Remove it |
| undefined or NaN | {"a":undefined} | Use null |
| Bad escape | "C:\path" | Double the backslash |
These account for nearly every invalid JSON message you will see. The rest are structural: an unclosed bracket, a missing comma between items, or two values where one was expected.
Paste the JSON to see the exact error position.
Open the JSON Formatter and Validator →Why the rules are strict
JSON was designed to be parsed the same way everywhere, with no optional features. Comments, trailing commas and single quotes are all features that different parsers might handle differently, so the specification leaves them out.
JavaScript object literals allow several of these, which is why the errors are so common: people paste a JavaScript object into a JSON field.
Reading the error message
Parsers report the position where parsing failed, which is often just after the real mistake. A message pointing at a closing brace usually means the item before it was wrong.
If the position is not helpful, format the document first. Errors are much easier to spot in an indented structure than in one long line.
Escaping backslashes and quotes
A backslash inside a string must be doubled. A Windows path such as C:\Users\name becomes "C:\\Users\\name" in JSON.
A double quote inside a string must be escaped with a backslash. Copying text that contains quotes without escaping is a common source of failure.
Numbers, booleans and null
Only lowercase true, false and null are valid. True, FALSE and None are not, and they are easy to introduce when hand-editing.
Numbers cannot have leading zeros, cannot contain a comma and cannot be NaN or Infinity. Use strings for values that look numeric but are not, such as phone numbers.
Structure errors
A missing comma between two items, an unclosed bracket or brace, or a stray character after the closing brace are the structural failures.
Count the opening and closing brackets. If the counts differ, the structure is broken somewhere, and the position of the mismatch is usually obvious once counted.
Validating before sending
Any formatter or validator will catch all six. Run the payload through one before sending it, rather than discovering the problem from a 400 response.
In code, parse the string as part of your tests. A test that parses every fixture catches these errors before they reach an environment.
When you cannot change the source
If a system emits invalid JSON, the practical fix is usually a small transformation before parsing: strip comments, remove trailing commas, replace single quotes. Do it in one place and document it.
That is a workaround, not a solution. Where you control the source, fix the source.
Common mistakes
- Assuming a JavaScript object is JSON.
- Leaving a trailing comma after the last item.
- Using a comment, which is never valid JSON.
- Forgetting to escape backslashes in paths.
- Using undefined instead of null.
A checklist
Format it. Check the six. Count the brackets. Parse it. If it still fails, look at the position the parser reports and read the item immediately before it.
Related formats
JSON5 and JSONC allow comments and trailing commas. They are useful for configuration files and are not valid JSON for an API.
If you are writing configuration by hand, check whether the tool accepts JSONC before adding comments to a .json file.
A test that catches these early
Parse every JSON fixture in your test suite. A single test that runs the parser over each example catches all six of these errors before they reach an environment.
It is a few lines of code and it removes a whole category of production failures.
Frequently asked questions
Can JSON have comments?
No. Standard JSON does not allow comments, though some tools accept a relaxed variant.
Why does my JSON fail with a trailing comma?
The specification does not permit a comma after the last item.
Is a single quote valid in JSON?
No. Strings and keys must use double quotes.