How to Fix Invalid JSON
Fix invalid JSON caused by trailing commas, single quotes, comments, and unquoted keys. See before-and-after examples and how to validate JSON in your browser.
Find the first syntax error, then re-parse
A formatter can only pretty-print JSON that already parses. If you see Unexpected token, Expected property name, or JSON.parse throwing, fix the text at the reported line — usually a trailing comma, a single-quoted string, an unquoted key, or a comment.
Paste the payload into the JSON Formatter & Minifier to validate it in your browser. The tool reports line and column when the parser provides them. After it parses, use Format if you need indentation. For the pretty-print workflow, see How to Format JSON Online.
JSON is not a JavaScript object literal
Most “invalid JSON” is valid JavaScript. JSON (RFC 8259) is stricter: double-quoted keys and strings, no comments, no trailing commas, and no undefined, NaN, or Infinity.
Trailing commas
A comma after the last property or array item is the most common copy-paste failure — especially from TypeScript objects or Python dicts printed with extra commas.
{
"id": 18,
"role": "editor",
}Fixed trailing comma
Remove the last comma. Do not add a dummy property just to “use” the comma.
{
"id": 18,
"role": "editor"
}Single quotes and unquoted keys
JSON strings and object keys must use double quotes. Single quotes and bare identifiers are JavaScript only.
{
name: 'Ada',
active: true
}Fixed quotes and keys
Quote every key. Switch string values to double quotes. Escape any double quote that belongs inside a string with \".
{
"name": "Ada",
"active": true
}Comments are not allowed
// and /* */ are fine in many config dialects (JSONC) and in JavaScript. Standard JSON has no comment syntax. Strip comments, or keep a documented JSONC file and compile it to JSON in your build — do not ship comments to JSON.parse.
{
"timeoutMs": 5000,
// retry on 429
"retries": 3
}undefined, NaN, and trailing commas in nested objects
JSON’s empty value is null. undefined is not a JSON token. NaN and Infinity are not JSON numbers. A trailing comma buried in a nested object still fails the whole document — parsers stop at the first error, which is why nested payloads feel “randomly broken.”
{
"user": {
"id": 7,
"label": undefined,
},
"score": NaN
}A valid nested example
Replace undefined with null or omit the key. Use a number or null instead of NaN.
{
"user": {
"id": 7,
"label": null
},
"score": null
}How to work through a parser error
Do not re-indent until the document parses. Extra whitespace will not fix a trailing comma.
- Read the line and column from the error. Jump there first.
- Fix one issue, then parse again. Later errors often disappear.
- If the file is huge, copy the object around the reported line into a smaller fixture.
- If two versions look the same, compare the text to catch a smart quote or a hidden comma.
Validate, then format
Use the JSON Formatter & Minifier to parse and, once valid, pretty-print or minify. If the next step is a spreadsheet, convert a JSON array with the JSON to CSV Converter after the JSON is valid.
Frequently asked questions
- Why is my JSON invalid?
- The usual causes are trailing commas, single quotes, unquoted keys, comments, or non-JSON values such as undefined and NaN. JSON is stricter than JavaScript object syntax.
- Can a JSON formatter fix invalid JSON automatically?
- Not reliably. A formatter needs a successful parse. Some editors offer “JSON with comments” or repair heuristics; standard JSON.parse will still reject the original text.
- What does Unexpected token mean in JSON?
- The parser hit a character it did not expect at that position — often a comma, a single quote, a slash from a comment, or a bare identifier used as a key.
- Are trailing commas allowed in JSON?
- No. RFC 8259 does not allow a comma after the last array element or object member. Some JSON5/JSONC parsers accept them; APIs and JSON.parse do not.