UnblockDevs

How to Fix JSON Syntax Errors — Every Common JSON Error Explained & Fixed

"Unexpected token", "Expected comma or ]", "Unterminated string" — JSON parse errors are cryptic, and the error position often points to the wrong place. This guide covers every common JSON syntax error developers hit: what causes it, what the error message actually means, and exactly how to fix it. Plus the tools that catch and auto-fix most errors in one click.

#1

Trailing comma — the most common JSON error from JS developers

← -1

Error position is often one character past the actual mistake

Auto-fix

Most common errors can be repaired automatically in one click

1

How JSON Error Positions Work — Read This First

JSON parse errors report the position where the parser gave up, not always where you made the mistake. The parser reads left-to-right and stops at the first character it cannot handle. The actual bug is often one character earlier.

Error position is where the parser failed — not where you made the mistake

If you see Unexpected token } in JSON at position 42, the } at position 42 is where the parser stopped. The real problem is usually the character just before it — a trailing comma after the last property that made the parser expect another property when it saw } instead. Always look one to two characters before the reported error position.
2

Error 1: Trailing Comma (Most Common)

Trailing comma after last property or array element

Invalid — trailing comma causes Unexpected token } or ] error

❌ Bad
// JSON.parse error: Unexpected token } in JSON at position 28
{
  "name": "Alice",
  "age": 30,       ← trailing comma — parser expects another property
}

// Also fails in arrays:
// JSON.parse error: Unexpected token ] in JSON at position 15
["apple", "banana",]  ← trailing comma before ]

Valid — no comma after last property or element

✅ Good
// No comma after the last property
{
  "name": "Alice",
  "age": 30
}

// No comma after the last array element
["apple", "banana"]

Why this happens — JavaScript allows trailing commas, JSON does not

JavaScript object literals and arrays allow trailing commas since ES5: { a: 1, b: 2, } is valid JS. Developers writing JSON by hand often apply the same habit. But the JSON spec (RFC 8259) explicitly forbids trailing commas — every comma must be followed by another value.

3

Error 2: Single Quotes Instead of Double Quotes

Single-quoted strings — valid JavaScript, invalid JSON

Invalid — single quotes not allowed in JSON

❌ Bad
// JSON.parse error: Unexpected token ' in JSON at position 1
{
  'name': 'Alice',
  'city': 'London'
}
// Both keys AND values must use double quotes in JSON

Valid — all strings use double quotes

✅ Good
{
  "name": "Alice",
  "city": "London"
}
4

Error 3: Unquoted Property Keys

Unquoted keys — valid in JavaScript objects, invalid in JSON

Invalid — property keys must be quoted strings

❌ Bad
// JSON.parse error: Unexpected token n in JSON at position 4
{
  name: "Alice",     ← unquoted key
  age: 30,
  active: true
}

Valid — all keys are double-quoted strings

✅ Good
{
  "name": "Alice",
  "age": 30,
  "active": true
}
5

Error 4: Comments — Not Allowed in JSON

JavaScript comments in JSON — common in config files

Invalid — comments (// and /* */) not allowed in JSON

❌ Bad
// JSON.parse error: Unexpected token / in JSON at position 0
// App configuration
{
  "theme": "dark",
  /* display settings */
  "language": "en",
  "debug": false  // set to true for verbose logging
}

Valid — no comments; use JSONC or JSON5 if you need comments

✅ Good
{
  "theme": "dark",
  "language": "en",
  "debug": false
}
// Comments must be removed before parsing standard JSON
// If you need comments: use JSONC (.jsonc files) or JSON5 format

JSONC and JSON5 support comments — tsconfig.json is JSONC

TypeScript config files (tsconfig.json) are actually JSONC (JSON with Comments) — parsed by a special JSONC parser, not standard JSON.parse(). VS Code also supports JSONC in settings.json. These are not standard JSON and will fail in any JSON.parse() call. Use JSON5 (json5 npm package) if you need comments in JSON files you parse in code.

6

Error 5: Missing Commas Between Properties

Missing comma between properties — parser expects } or , but finds a key

Missing comma after "Alice" — parser sees second key unexpectedly

❌ Bad
// JSON.parse error: Unexpected string in JSON at position 22
{
  "name": "Alice"   ← missing comma here
  "age": 30,
  "city": "London"
}

Commas after every property except the last one

✅ Good
{
  "name": "Alice",
  "age": 30,
  "city": "London"
}
7

Error 6: Mismatched or Missing Brackets/Braces

Array closed with } or object closed with ] — mismatched brackets

Mismatched bracket — } closes the object but the array [ is still open

❌ Bad
// JSON.parse error: Expected , or ] in JSON at position 58
{
  "users": [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
  }    ← should be ] to close the array, not }
}

] closes the array correctly, then } closes the outer object

✅ Good
{
  "users": [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
  ]
}
javascriptTrack nesting depth to find mismatched brackets
// Debugging tip: count brackets manually for small JSON
function checkBrackets(json) {
  const stack = [];
  const pairs = { '{': '}', '[': ']' };
  const closing = new Set(['}', ']']);

  for (let i = 0; i < json.length; i++) {
    const char = json[i];
    if (pairs[char]) {
      stack.push({ char, pos: i });
    } else if (closing.has(char)) {
      const last = stack.pop();
      if (!last || pairs[last.char] !== char) {
        return `Mismatch at position ${i}: found ${char}, expected ${last ? pairs[last.char] : 'nothing'}`;
      }
    }
  }

  if (stack.length > 0) {
    const { char, pos } = stack[stack.length - 1];
    return `Unclosed ${char} at position ${pos}`;
  }
  return 'Brackets balanced';
}
8

Error 7: Unterminated String

String that never closes — missing closing quote

Unterminated string — parser reads to end of line without finding closing "

❌ Bad
// JSON.parse error: Unterminated string in JSON at position 10
{
  "name": "Alice Smith,   ← missing closing " before comma
  "age": 30
}

String properly closed with double quote before the comma

✅ Good
{
  "name": "Alice Smith",
  "age": 30
}
9

Error 8: Invalid Escape Sequences in Strings

Backslash in string values — must be escaped

Unescaped backslash — parse error in paths and regex patterns

❌ Bad
// JSON.parse error: Bad escape character in string literal
{
  "path": "C:UsersAliceDocuments",    ← backslashes not escaped
  "regex": "d+.d+"
}

Backslash escaped as \\\\ — valid in JSON strings

✅ Good
{
  "path": "C:\\Users\\Alice\\Documents",
  "regex": "\\d+\\.\\d+"
}
// In JSON strings: backslash must be written as \
// Valid escape sequences: \\ \" \/ \b \f \n \r \t \uXXXX

Valid JSON escape sequences

\\ (backslash), \" (double quote), \/ (forward slash), \b (backspace), \f (form feed), \n (newline), \r (carriage return), \t (tab), \uXXXX (Unicode codepoint). Any other backslash sequence is invalid.

Forward slash is optionally escaped

\/ is valid but not required. JSON.stringify() does not escape forward slashes. Some output from other systems may include \/ for HTML safety — this is valid JSON.

Newlines inside strings must be \n

Literal newlines inside a JSON string value (pressing Enter inside the string) are invalid JSON. Represent newlines as \n in the string value.

Unicode: use \uXXXX

Non-ASCII characters can appear directly in JSON (UTF-8 is valid) or as \uXXXX escape sequences. Both are valid. JSON.stringify() with default settings keeps non-ASCII characters as-is.

10

Auto-Fix JSON Errors in One Click

For common errors — trailing commas, single quotes, unquoted keys, and comments — the JSON Beautifier auto-fix feature repairs the JSON automatically without you reading every error.

1

Paste the broken JSON

Go to unblockdevs.com/json-beautifier or unblockdevs.com/json-validator. Paste the invalid JSON. The error is highlighted immediately with exact line and column numbers.

2

Click Auto-Fix

The auto-fix feature corrects: trailing commas, single-quoted strings, unquoted keys, and JavaScript comments. Click once and the JSON is repaired.

3

Review the fixed output

The corrected JSON appears in the output panel, formatted and valid. Review the diff to understand what was changed.

4

For complex errors, use the line:column position

If auto-fix cannot repair the JSON (mismatched brackets, unterminated strings), use the exact line and column number from the error report to find and fix the issue manually.

Frequently Asked Questions