Fix Invalid JSON: Common Errors and How to Repair Them

Invalid JSON almost always falls into one of six categories: trailing commas, wrong quote style, unquoted keys, missing brackets, comments, or bad escape sequences. This guide covers each one with a broken example and the corrected version.

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