UUtilityApp

Common JSON Errors and How to Fix Them

JSON looks simple until it isn't. A single trailing comma, a stray byte-order mark, or a JavaScript-style comment will cause a parser to reject the entire document with a message that often tells you almost nothing useful. This guide covers the six errors that account for the vast majority of real-world JSON failures, shows you exactly what the broken and fixed versions look like, and explains a line-by-line debugging approach that finds the culprit in seconds.

Outil gratuit
JSON Formatter & Validator

Why JSON Is So Unforgiving

JSON (JavaScript Object Notation) is defined by ECMA-404 and RFC 8259. Both specs are deliberately strict: no comments, no trailing commas, no single-quoted strings, no undefined or NaN values. This strictness is a feature — it means any conforming parser on any platform will read the same document the same way. The downside is that every deviation, no matter how small, is a hard error.

Most parsers stop at the first error and report a byte offset or a line number. That line number often points to where the parser gave up, not where the mistake actually is. Understanding the common error categories lets you jump straight to the real cause.

The Six Errors That Break JSON (and Their Fixes)

ErrorBroken exampleFixed exampleRoot cause
Trailing comma{"a":1,}{"a":1}Valid in JS, illegal in JSON
Single-quoted strings{'key':'val'}{"key":"val"}JSON requires double quotes
Unquoted keys{key:"val"}{"key":"val"}All keys must be quoted strings
BOM prefix{"a":1}{"a":1}UTF-8 BOM is not valid JSON whitespace
NaN / Infinity{"x":NaN}{"x":null} or omitThese are JS primitives, not JSON values
Comments{// note "a":1}{"a":1}JSON has no comment syntax

Trailing Commas

This is the single most common JSON error. JavaScript, Python dicts, and many config formats accept a comma after the last item in an array or object. JSON does not.

Broken:

["apple", "banana", "cherry",]

Fixed:

["apple", "banana", "cherry"]

The trailing comma problem is especially sneaky when you're building JSON by hand or templating it in code, because it only appears when the last element is removed or when the list has exactly one item. A formatter that validates on paste will flag this immediately.

Single Quotes and Unquoted Keys

JavaScript object literals accept single quotes and bare (unquoted) keys. Developers writing JSON by hand frequently slip into JS syntax without noticing.

Broken:

{name: 'Alice', age: 30}

Fixed:

{"name": "Alice", "age": 30}

Every key in a JSON object must be a double-quoted string, and every string value must also use double quotes. Single quotes are never valid anywhere in a JSON document. If your source data comes from a JavaScript console.log of an object, remember that the browser's output is a JS representation, not valid JSON — use JSON.stringify(obj) instead.

The UTF-8 BOM

A Byte Order Mark (BOM) is a three-byte sequence (EF BB BF in hex) that some Windows editors — notably older versions of Notepad — prepend to UTF-8 files to signal the encoding. RFC 8259 explicitly forbids a BOM at the start of a JSON document.

The resulting parse error is baffling if you don't know to look for it: the parser sees an unexpected character at position 0 even though the file looks perfectly normal in your text editor (the BOM is invisible). The fix is to strip the BOM. Most modern editors have a "Save without BOM" option. Pasting the content into a formatter and re-saving through it will usually strip the BOM automatically.

NaN, Infinity, and undefined

JSON's number type covers finite IEEE 754 double-precision values. NaN, Infinity, and -Infinity are IEEE 754 concepts but they are not part of the JSON spec. undefined likewise exists in JavaScript but has no JSON equivalent.

Broken:

{"ratio": NaN, "limit": Infinity, "token": undefined}

Fixed options:

  • Replace NaN / Infinity with null if the absence of a valid number needs to be signaled.
  • Omit the key entirely if undefined semantics are intended (JSON serializers like JSON.stringify already drop undefined values by default).
  • Encode them as strings ("NaN", "Infinity") if the consumer needs to distinguish them from null — but document that convention explicitly.

If you're seeing NaN in JSON generated by a library, look for floating-point division by zero or 0/0 in the upstream calculation.

Line-by-Line Debugging Approach

When a parser gives you a vague error like "Unexpected token at position 1847", work through these steps in order:

  1. Paste into a formatter first. A good JSON formatter will highlight the exact line and character. This alone resolves most issues in under ten seconds.
  2. Check the very first character. If position 0 or 1 is flagged, suspect a BOM or a leading whitespace character that isn't actually whitespace (some copy-paste operations introduce non-breaking spaces, U+00A0, which JSON parsers reject).
  3. Search for single quotes. A quick Ctrl+F for ' in your editor will find all single-quoted strings. Replace them with double quotes, making sure to escape any double quotes inside the value with a backslash: "it\'s" becomes "it's" — wait, the correct form is "it's" (apostrophe needs no escaping in a JSON string, only " and \ do).
  4. Search for trailing commas. The regex ,\s*[}\]] matches any comma immediately before a closing brace or bracket. Run it across the whole document.
  5. Bisect the document. If the error is still elusive, delete the second half of the document (keeping it valid with a closing bracket) and check if the error moves. Repeat until you've isolated the offending block to fewer than 20 lines.
  6. Validate the structure, not just syntax. A structurally valid but semantically wrong document (e.g., a number where an array is expected) will pass JSON validation but fail at runtime. Use a schema validator if the downstream system provides a JSON Schema.

Questions fréquentes

Why does JSON not allow trailing commas when JavaScript does?+

JSON was derived from a subset of JavaScript syntax circa 2001, before trailing commas were widely accepted. The spec was frozen to maximize cross-language interoperability, so it kept the stricter rule. JavaScript itself only officially permitted trailing commas in object and array literals in ES5 (2009) and in function parameters in ES2017.

Can I use comments in JSON?+

No. The JSON spec has no comment syntax. If you need annotated configuration files, consider JSONC (JSON with Comments, used by VS Code settings), JSON5, or YAML. When you must use plain JSON, move documentation into a dedicated key like "_comment" — though that pollutes the data model and should be used sparingly.

What is the correct way to represent a date in JSON?+

JSON has no date type. The near-universal convention is to encode dates as ISO 8601 strings, for example "2026-06-11T14:30:00Z". This format sorts lexicographically, is unambiguous, and is supported by virtually every JSON-aware library's date parser.

My JSON looks correct but the parser still fails — what else could cause this?+

Three less-obvious causes: (1) a non-breaking space (U+00A0) or zero-width character embedded in a key or string — these are invisible in most editors but invalid in JSON strings outside a Unicode escape; (2) a numeric value that exceeds the safe integer range (beyond 2^53 - 1), which some parsers reject or silently truncate; (3) deeply nested structures that exceed the parser's recursion limit, which is implementation-defined and can be as low as 500 levels in some environments.

How do I fix JSON that was generated by Python's repr() instead of json.dumps()?+

Python's repr() uses single quotes and True/False/None instead of JSON's true/false/null. Never use repr() for JSON output. Always use json.dumps(obj) from the standard library, which produces spec-compliant JSON. If you already have a repr() string, a formatter with auto-fix can handle the quote conversion, but True/False/None replacements need to be done carefully to avoid touching string contents.