Back to blog
Guides3 min readVektosys

How to Fix Invalid JSON: A Field Guide to Common Errors

Diagnose and repair invalid JSON—trailing commas, quotes, brackets, and encoding issues—with clear examples and a repair workflow.

Stay calm: invalid means unparsable

When a parser rejects JSON, something in the text violates the grammar. Your job is to locate the defect, fix it, and re-validate. Guessing wildly wastes time; a systematic approach does not.

Step 1: Get a precise error

Paste the document into a validator that reports line and column (or character offset). Read the message literally. “Expected ',' or '}'” usually means a missing comma or an extra trailing comma nearby—not a mysterious encoding curse.

Browser-local validators are ideal for customer data and tokens. JSON Editor is built for quick paste-validate-fix loops without uploading files.

Trailing commas

Invalid:

{
  "a": 1,
  "b": 2,
}

Valid: remove the comma after 2.

Trailing commas are legal in modern JavaScript—and illegal in JSON. That mismatch causes endless confusion.

Quotes and keys

Invalid:

{
  name: "Sam",
  'role': "admin"
}

Valid:

{
  "name": "Sam",
  "role": "admin"
}

Keys and strings use double quotes only.

Smart quotes from documents

Word processors convert "plain" into “curly”. JSON parsers reject curly quotes. Re-type quotes in a plain text editor or run a replace from Unicode quotation marks to ASCII " .

Brackets and braces

Count openings and closings. A missing ] after an array of objects is extremely common when editing by hand:

{
  "items": [
    {"id": 1},
    {"id": 2}
}

The array never closes. Pretty-printing after a partial fix often reveals the imbalance visually.

Unescaped characters in strings

Inside strings you must escape:

  • " as \"
  • \ as \\
  • control characters via \n, \t, etc.

Raw line breaks inside a string value are invalid. Use \n instead.

Comments and JavaScript leftovers

{
  // user id
  "id": 1
}

Comments are not standard JSON. Strip them or use a JSONC-aware toolchain intentionally—and do not ship JSONC to strict parsers expecting JSON.

Numbers and literals

NaN, Infinity, and undefined are not valid JSON. Use null, omit the key, or encode a string if you must preserve a sentinel—then document it.

Repair workflow summary

  1. Validate; note position.
  2. Inspect ±5 lines around the error.
  3. Fix the smallest thing (comma, quote, bracket).
  4. Validate again; repeat until green.
  5. Pretty-print and skim for structural sense.
  6. Only then apply schema or business checks.

Prevention beats repair

Generate JSON from serializers whenever possible. When hand-editing is required, edit pretty-printed files and validate before committing. Keep fixtures in CI so invalid JSON never merges quietly.

Invalid JSON is rarely mystical. It is almost always a comma, a quote, or a bracket—find it with a good error position, fix it once, and move on.

Related articles