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.
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
Trailing comma causes parse error
SyntaxError: Unexpected token } in JSON at position 42
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 Paste your JSON into the formatter input.
- 2 Click Format — the error indicator highlights the problem line.
- 3 Find the last property before a closing `}` or `]`.
- 4 Remove the comma after that last property.
- 5 Click Format again to confirm the fix.
{
"name": "Alice",
"age": 30,
}
{
"name": "Alice",
"age": 30
}
Unquoted object keys
SyntaxError: Unexpected token n in JSON at position 1
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 Identify all keys in your object.
- 2 Wrap each key in double quotes.
- 3 Replace any single-quoted strings with double-quoted strings.
- 4 Re-run the formatter.
{name: 'Alice', age: 30}
{"name": "Alice", "age": 30}
Single quotes instead of double quotes
SyntaxError: Unexpected token ' in JSON at position 9
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 Use your editor's find-and-replace with regex enabled.
- 2 Replace `'([^']*)'` with `"$1"` for string values.
- 3 Be careful with apostrophes inside strings — escape them as `\'` if needed.
- 4 Validate in the formatter after replacement.
{'status': 'ok', 'count': 5}
{"status": "ok", "count": 5}
undefined, NaN, or Infinity values
SyntaxError: Unexpected token u in JSON at position 10
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 Replace `undefined` with `null`.
- 2 Replace `NaN` with `null` or a sentinel like `0`.
- 3 Replace `Infinity` or `-Infinity` with `null` or a large number constant.
- 4 In JS code, use `JSON.stringify(obj, (k, v) => Number.isFinite(v) ? v : null)` as a replacer.
{"score": NaN, "limit": Infinity, "value": undefined}
{"score": null, "limit": null, "value": null}
Unescaped control characters in strings
SyntaxError: Bad control character in string literal in JSON at position 18
Control characters (tab `\t`, newline `\n`, carriage return `\r`, etc.) must be escaped when they appear inside JSON strings.
Step-by-step fix
- 1 Use the formatter's error highlight to find the problematic string.
- 2 Replace literal newlines inside strings with `\n`.
- 3 Replace literal tabs with `\t`.
- 4 In production code, always use `JSON.stringify()` instead of building JSON strings manually.
{"message": "Hello
World"}
{"message": "Hello\nWorld"}
Duplicate keys silently override data
(No error thrown — data is silently lost)
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 Search for duplicate keys using the formatter's validation output.
- 2 Decide which value is authoritative and remove the duplicate.
- 3 If both values are needed, merge them into an array or rename one key.
{"id": 1, "id": 2, "name": "Alice"}
{"id": 2, "name": "Alice"}
Large integers lose precision
(No error — number silently rounds to nearest float64)
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 Identify large integer fields (IDs, timestamps in nanoseconds).
- 2 Convert them to strings in your API response.
- 3 Parse them back with BigInt() in JavaScript: `BigInt(str)`.
{"id": 9007199254740993}
{"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 →