DevFormatLab
← Back to blog

Common JSON Errors and How to Fix Them

By DevFormatLab·8 min read
JSONDebuggingJavaScriptTutorial

JSON (JavaScript Object Notation) is everywhere — APIs, config files, NoSQL databases, data exchange. But when it breaks, the error messages can be cryptic. This guide walks through the most common JSON errors and exactly how to fix them.

1. SyntaxError: Unexpected token

This is the most common JSON error. It means the parser hit a character it didn't expect at that position.

Common causes:

  • Trailing commas: {"name": "Alice",} — that comma after "Alice" is invalid in JSON (even though JS accepts it)
  • Single quotes: JSON requires double quotes: {'name': 'Alice'} is invalid. Must be {"name": "Alice"}
  • Unquoted keys: {name: "Alice"} — JSON keys must be quoted
  • Comments: JSON doesn't support // or /* */ comments

How to fix:

Paste your JSON into our JSON Formatter — it will point you directly to the line and column where the error occurs. The character offset in the error message is usually accurate.

2. SyntaxError: Unterminated string

A string literal wasn't closed properly. The parser reached the end of input while still looking for the closing quote.

Common causes:

  • Missing closing quote: {"name": "Alice"}
  • Unescaped quote inside string: {"quote": "He said \"hello\""} — should be "He said \\\"hello\\\""
  • Newline inside string: JSON strings can't span multiple lines unless you escape the newline

How to fix:

Look at the position reported by the error. The actual mistake is often just before that position. Our JSON Formatter will highlight the unterminated region.

3. SyntaxError: Unexpected end of JSON input

The parser reached the end of the file before the JSON structure was complete.

Common causes:

  • Truncated network response (connection dropped mid-transfer)
  • Missing closing brace/bracket: {"users": [{"name": "Alice"} — missing closing braces
  • Incomplete file write

How to fix:

Count your opening and closing braces/brackets. A good strategy is to indent properly and then check that the indentation makes sense. Our formatter's tree view will show you where the structure terminates prematurely.

4. ReferenceError: xxx is not defined

This happens when you try to use JSON directly in JavaScript without quoting it as a string first.

// ❌ Wrong — parser sees literal braces, not a string
const data = JSON.parse({"name": "Alice"});

// ✅ Right — pass a string
const data = JSON.parse('{"name": "Alice"}');

How to fix:

Remember: JSON.parse() takes a string containing JSON. If you're pasting JSON into JS source code, you need the outer quotes and the inner quotes escaped. Our JSON → String tool handles this automatically.

5. Numbers with leading zeros

JSON doesn't allow leading zeros on numbers unless the number is exactly "0".

  • {"count": 042} → INVALID
  • {"count": 42} → ✓ valid
  • {"count": "042"} → ✓ valid (it's a string)

6. Invalid escape sequences

JSON only recognizes these escape sequences: \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX unicode escapes. Anything else like \e or \x1B is invalid.

Pro Tips for Debugging JSON

  1. Use the formatter first — paste into our JSON Formatter to get precise line/column positions
  2. Work from the outside in — check the outermost structure closes properly before debugging inner objects
  3. Validate incrementally — if you have a large JSON file, remove sections and re-validate to isolate which part contains the error
  4. Compare against valid JSON — use JSON Diff to compare your malformed JSON against a known-good template

When JSON Validates But Still Doesn't Work

Sometimes JSON is syntactically valid but semantically wrong. For example:

  • String vs number mismatch: "42" vs 42 — the API expects a number but got a string
  • Null vs undefined: JSON has null, not undefined — omitting a key is different from setting it to null
  • Case sensitivity: "Username" vs "username" — JSON keys are case-sensitive

For these cases, use our JSON Diff tool to compare your payload against a working example side-by-side.


All tools mentioned in this article run 100% in your browser — your data never leaves your device. Happy debugging!

Related Tools