← Guides

Common JSON Syntax Errors (and How to Fix Them)

JSON is not JavaScript

Most invalid JSON comes from writing a JavaScript object literal and expecting it to parse. JSON is a stricter, smaller format. These five things are valid in JavaScript but illegal in JSON.

1. Trailing commas

{ "a": 1, "b": 2, }   // invalid JSON
{ "a": 1, "b": 2 }    // valid

2. Single quotes or unquoted keys

{ name: 'Ann' }    // invalid JSON
{ "name": "Ann" }  // valid

All keys must be double-quoted strings, and all string values must use double quotes.

3. Comments

{ "a": 1 }  // this comment makes the document invalid

JSON has no comment syntax. If your configuration format accepts comments, it is probably JSON5, JSONC or YAML rather than strict JSON. Strip comments before handing the data to a strict parser.

4. undefined, NaN and Infinity

These are JavaScript values with no JSON representation. JSON.stringify silently drops undefined object properties and turns NaN into null, which is how "my data disappeared after saving" bugs happen. Use null explicitly.

5. Unescaped characters inside strings

A raw line break, tab or unescaped double quote inside a string breaks parsing. Use \n, \t and \" instead.

How to find the error quickly

  1. Paste the document into a validator and read the reported offset — the message usually names the offending token.
  2. Format the valid part: pretty-printing makes mismatched brackets obvious, because indentation stops increasing where it should.
  3. Check bracket balance. A missing ] or } is the most common cause of "end of data" errors.
  4. For very large files, minify first and validate again; minified output tells you whether the problem is syntax or size limits.

Formatting vs minifying

Indented JSON is for humans: debugging, code review, documentation. Minified JSON is for transport: no spaces or line breaks, smaller payloads. Both are semantically identical to a parser, so choose based on who reads it.

Where JSON shows up

JSON is the default wire format for almost every modern API, the storage format for configuration and package manifests, and the payload inside webhooks, logs and JSON Web Tokens. A parser that rejects one byte stops the whole document, so a single trailing comma in a large configuration file can take down a deployment that otherwise looks correct. The format is deliberately small: the specification defines six value types and a handful of grammar rules, and nothing else is legal.

The grammar in one screen

value   = object | array | string | number | true | false | null
object  = { "key": value, ... }
array   = [ value, ... ]
string  = "text with \" or \\ or \n or \u00e9 escapes"
number  = -? digits (. digits)? (e|E sign? digits)?

Everything else is an error. Keys must be strings, so an unquoted name is invalid even though it is a common JavaScript shorthand. Numbers cannot start with a plus sign, cannot have leading zeros such as 01, cannot be written as .5 or 1., and have no representation for hexadecimal, NaN or Infinity. Strings must use double quotes; the escape sequences are limited to a fixed set, and a \u escape needs exactly four hexadecimal digits.

Problems beyond the usual five

  • Duplicate keys — strictly legal, but the last value wins in most parsers, which makes a document look like it contains one value while the parser sees another.
  • A byte-order mark — a BOM at the start of a UTF-8 file is invalid JSON and produces a confusing “unexpected character” at position 0.
  • Control characters — a raw tab or newline inside a string must be escaped, and so must any character below U+0020.
  • Truncated documents — a response cut off by a timeout ends mid-object and reports “unexpected end of input” rather than a clear problem.
  • Wrong encoding — JSON must be UTF-8 when exchanged between systems; a Windows-1252 file with accented text can fail on the first non-ASCII byte.

Decoding the error message

MessageLikely cause
Unexpected token } at position NTrailing comma, or a missing value before the brace
Unexpected end of JSON inputMissing closing bracket, or a truncated file
Unexpected token o in JSON at position 1The text is a JavaScript object, not JSON — it often starts with the word object
Unexpected character at position 0A BOM, a leading comment, or an HTML error page returned instead of data
Expected property name with double quotesAn unquoted key or a single-quoted key

Position numbers count bytes or code units from the start of the document, depending on the parser, so a browser error may point a little to the left of the actual problem when the file contains multi-byte characters.

Large files, streams and JSON Lines

Very large JSON is usually not one document. Logs, exports and event streams often use JSON Lines, where each line is a complete JSON value and no brackets surround the file. Parsing that as a single document fails at the first newline, and reading it fully into memory may exhaust the process. Line by line is the correct approach, with one parse per line so a bad record can be skipped and logged without discarding the rest. For a single huge document, a streaming parser that emits events is the alternative to loading the whole file.

When the format is not really JSON

Configuration files often look like JSON but allow comments and trailing commas. Those are JSON5, JSONC or YAML, and they fail a strict parser even though the editor highlights them happily. Decide which format the file is meant to be, and keep the toolchain consistent: a .json file read by a strict parser must contain strict JSON, while a tool that accepts JSONC should say so explicitly in its documentation.

A practical checklist

  • Validate and format in one step before saving; a formatter refuses exactly the input a parser would refuse.
  • Keep strings double-quoted and remove trailing commas at the source, not in the file by hand.
  • Serialise with a library rather than building strings by concatenation — escaping is where hand-built JSON breaks.
  • Convert undefined, NaN and dates deliberately; JSON.stringify turns dates into strings and drops undefined properties.
  • Treat duplicate keys as a bug and enforce uniqueness in the producer.
  • Strip the BOM and declare UTF-8 when writing files.

Our JSON Formatter and Validator reports the failing position and repairs formatting without changing values. Related reading: JSON vs YAML for configuration and CSV to JSON conversion rules.

Try it yourself: open the JSON Formatter & Validator →