JSON 2026-03-10

Fix Common JSON Formatter Errors

Solve the most frequent JSON formatting and validation errors: trailing commas, unquoted keys, undefined values, circular references, and encoding issues.

📋 Tool: JSON Formatter — Free

JSON is deceptively strict. A single stray comma or unquoted key turns valid-looking data into a parse error. This guide covers the eight most common errors developers hit when using the JSON Formatter tool and exactly how to fix each one.

Jump to error

  1. 1 Trailing comma causes parse error
  2. 2 Unquoted object keys
  3. 3 Single quotes instead of double quotes
  4. 4 undefined, NaN, or Infinity values
  5. 5 Unescaped control characters in strings
  6. 6 Duplicate keys silently override data
  7. 7 Large integers lose precision
1

Trailing comma causes parse error

Error message
SyntaxError: Unexpected token } in JSON at position 42
Root cause

JavaScript and most JSON parsers reject trailing commas after the last item in an array or object. JSON specification (RFC 8259) explicitly forbids them.

Step-by-step fix

  1. 1 Paste your JSON into the formatter input.
  2. 2 Click Format — the error indicator highlights the problem line.
  3. 3 Find the last property before a closing `}` or `]`.
  4. 4 Remove the comma after that last property.
  5. 5 Click Format again to confirm the fix.
Wrong
{
  "name": "Alice",
  "age": 30,
}
Correct
{
  "name": "Alice",
  "age": 30
}

2

Unquoted object keys

Error message
SyntaxError: Unexpected token n in JSON at position 1
Root cause

JSON requires all object keys to be double-quoted strings. JavaScript object literal syntax (unquoted or single-quoted keys) is not valid JSON.

Step-by-step fix

  1. 1 Identify all keys in your object.
  2. 2 Wrap each key in double quotes.
  3. 3 Replace any single-quoted strings with double-quoted strings.
  4. 4 Re-run the formatter.
Wrong
{name: 'Alice', age: 30}
Correct
{"name": "Alice", "age": 30}

3

Single quotes instead of double quotes

Error message
SyntaxError: Unexpected token ' in JSON at position 9
Root cause

JSON mandates double quotes for both keys and string values. Single quotes are valid in JavaScript but invalid in JSON.

Step-by-step fix

  1. 1 Use your editor's find-and-replace with regex enabled.
  2. 2 Replace `'([^']*)'` with `"$1"` for string values.
  3. 3 Be careful with apostrophes inside strings — escape them as `\'` if needed.
  4. 4 Validate in the formatter after replacement.
Wrong
{'status': 'ok', 'count': 5}
Correct
{"status": "ok", "count": 5}

4

undefined, NaN, or Infinity values

Error message
SyntaxError: Unexpected token u in JSON at position 10
Root cause

JSON supports only `null`, `true`, `false`, numbers, strings, arrays, and objects. JavaScript-specific values like `undefined`, `NaN`, and `Infinity` have no JSON equivalent.

Step-by-step fix

  1. 1 Replace `undefined` with `null`.
  2. 2 Replace `NaN` with `null` or a sentinel like `0`.
  3. 3 Replace `Infinity` or `-Infinity` with `null` or a large number constant.
  4. 4 In JS code, use `JSON.stringify(obj, (k, v) => Number.isFinite(v) ? v : null)` as a replacer.
Wrong
{"score": NaN, "limit": Infinity, "value": undefined}
Correct
{"score": null, "limit": null, "value": null}

5

Unescaped control characters in strings

Error message
SyntaxError: Bad control character in string literal in JSON at position 18
Root cause

Control characters (tab `\t`, newline `\n`, carriage return `\r`, etc.) must be escaped when they appear inside JSON strings.

Step-by-step fix

  1. 1 Use the formatter's error highlight to find the problematic string.
  2. 2 Replace literal newlines inside strings with `\n`.
  3. 3 Replace literal tabs with `\t`.
  4. 4 In production code, always use `JSON.stringify()` instead of building JSON strings manually.
Wrong
{"message": "Hello
World"}
Correct
{"message": "Hello\nWorld"}

6

Duplicate keys silently override data

Error message
(No error thrown — data is silently lost)
Root cause

The JSON spec says duplicate keys have undefined behavior. Most parsers keep the last value, silently discarding earlier ones. The formatter flags this as a warning.

Step-by-step fix

  1. 1 Search for duplicate keys using the formatter's validation output.
  2. 2 Decide which value is authoritative and remove the duplicate.
  3. 3 If both values are needed, merge them into an array or rename one key.
Wrong
{"id": 1, "id": 2, "name": "Alice"}
Correct
{"id": 2, "name": "Alice"}

7

Large integers lose precision

Error message
(No error — number silently rounds to nearest float64)
Root cause

JSON parsers in JavaScript use 64-bit floats (IEEE 754). Integers above 2^53−1 (9007199254740991) lose precision when parsed.

Step-by-step fix

  1. 1 Identify large integer fields (IDs, timestamps in nanoseconds).
  2. 2 Convert them to strings in your API response.
  3. 3 Parse them back with BigInt() in JavaScript: `BigInt(str)`.
Wrong
{"id": 9007199254740993}
Correct
{"id": "9007199254740993"}

Frequently Asked Questions

Why does my JSON look valid but the formatter still rejects it?

Common culprits are invisible characters (non-breaking spaces, BOM markers) copied from Word or PDF. Paste into a plain-text editor first, then into the formatter.

Can JSON have comments?

No. Standard JSON does not support comments. If you need comments, use JSON5 or JSONC format, or strip comments before parsing with a preprocessor.

Why does JSON.stringify() produce different output from the formatter?

JSON.stringify() serializes JavaScript objects which may include prototype properties or custom toJSON() methods. The formatter parses raw text — run the stringify output through the formatter to see if it matches.

Related Tools

Try the JSON Formatter now

Free, runs in your browser, no signup required. Learn more about JSON Formatter.

Open JSON Formatter →