Fix Invalid JSON: Common Errors and How to Repair Them

Last updated:

Invalid JSON falls into one of 6 categories: trailing commas, single quotes instead of double quotes, unquoted object keys, mismatched brackets, JavaScript-style comments, or bad escape sequences. The json-repair npm library (v0.12+) fixes the most common errors automatically with 1 function call, and online validators pinpoint the exact line and column of the first syntax error. This guide covers all 6 error types with before-and-after examples, json-repair for automated fixes, Python's JSONDecodeError messages, browser DevTools debugging, and converting JSON5 or JavaScript object literals to valid JSON.

Paste your broken JSON into Jsonic's Validator to see the exact line and character where it breaks before reading on.

Validate JSON now

1. Trailing commas

The most common mistake when hand-writing or editing JSON. JavaScript object literals allow trailing commas; JSON does not.

// ❌ Invalid
{
  "name": "Alice",
  "role": "admin",
}

// ✅ Valid
{
  "name": "Alice",
  "role": "admin"
}

The same rule applies inside arrays: ["a", "b",] is invalid. Remove the last comma before the closing bracket.

2. Single quotes instead of double quotes

JSON requires double quotes for both string values and object keys. Single quotes are valid in JavaScript but not in JSON.

// ❌ Invalid
{'name': 'Alice'}

// ✅ Valid
{"name": "Alice"}

If your payload came from a JavaScript console.log or Python's str() output, this is the most likely cause.

3. Unquoted keys

JSON object keys must be strings in double quotes. Bare identifiers are valid in JavaScript object literals but not in JSON.

// ❌ Invalid
{name: "Alice", age: 30}

// ✅ Valid
{"name": "Alice", "age": 30}

4. Comments

JSON has no comment syntax. If you are editing a config file that uses // ... or /* ... */, the format may be JSONC (JSON with Comments) rather than strict JSON. Strip all comments before parsing.

// ❌ Invalid JSON
{
  // user info
  "name": "Alice" /* admin user */
}

// ✅ Valid JSON
{
  "name": "Alice"
}

5. Missing or mismatched brackets

Every { needs a }, and every [ needs a ]. Truncated API responses and copy-paste errors are common causes.

// ❌ Invalid (missing closing brace)
{
  "user": {
    "name": "Alice"
  }

// ✅ Valid
{
  "user": {
    "name": "Alice"
  }
}

A JSON formatter with tree view makes mismatched brackets obvious because the nesting stops rendering correctly at the broken level.

6. Bad escape sequences

Inside a JSON string, backslashes must be escaped as \\. The only valid escape sequences are \", \\, \/, \n, \r, \t, \b, \f, and \uXXXX. Anything else is invalid.

// ❌ Invalid (unescaped backslash in Windows path)
{"path": "C:
ew	est"}

// ✅ Valid
{"path": "C:\new\test"}

Always generate JSON with JSON.stringify() rather than building strings manually to avoid this class of error entirely. See the JSON.stringify tutorial for safe patterns.

7. Undefined and NaN values

JSON does not support undefined, NaN, or Infinity. These are JavaScript-only values. JSON.stringify silently drops undefined keys and converts NaN and Infinity to null.

// ❌ Not valid JSON
{"value": undefined}
{"ratio": NaN}
{"limit": Infinity}

// ✅ Valid representations
{"ratio": null}
{"limit": null}

Quick reference: common errors

ErrorCauseFix
Unexpected token ','Trailing commaRemove last comma before } or ]
Unexpected token "'"Single quotesReplace with double quotes
Unexpected token identifierUnquoted keyWrap key in double quotes
Unexpected end of JSONMissing bracketAdd the missing } or ]
Bad escape characterUnescaped backslashReplace \ with \\

Fix your JSON in one step

Paste the broken payload into the JSON Formatter & Validator. It highlights the exact error position and reformats once you fix it.

Open JSON Validator

Frequently asked questions

What are the most common JSON syntax errors?

The top six: trailing commas, single quotes instead of double quotes, unquoted keys, comments, missing or mismatched brackets, and invalid escape sequences like an unescaped backslash.

How do I find where my JSON is invalid?

Paste it into a validator tool for a precise line and column number. In JavaScript, the SyntaxError from JSON.parse includes a position index you can use to slice the string around the error.

Can I fix JSON with comments automatically?

Use the strip-json-comments npm package — it removes only real comments and preserves // inside string values. A simple regex replacement is not safe.

What is the difference between JSON and JSON5?

JSON5 is a superset that allows single quotes, unquoted keys, trailing commas, and comments. It is useful for config files but not suitable for data interchange where strict JSON is required.

How do I fix unquoted keys in JSON?

Wrap each key in double quotes. Use the hjson or json5 npm package for reliable conversion rather than a regex, which can corrupt values containing colons.

What online tools can fix invalid JSON?

Jsonic's JSON Formatter identifies syntax errors inline and lets you fix them in the editor. The jsonrepair npm package automatically repairs trailing commas, single quotes, and unquoted keys for programmatic use.