Unquoted property names in JSON

ErrorJavaScript

SyntaxError: Expected property name or '}' in JSON at position 4 (line 2 column 3)

Where it stops

{
  name: "Ada",
  id: 7

What it means

In JSON a key is a string, so it needs double quotes, then a colon. There is no bare-identifier form, so {name: "Ada"} is invalid however ordinary the key looks.

Last verified on Chromium 145, Firefox 146, WebKit 26 and Node 20.

It opens with the error marked. Nothing is uploaded. Find where the error is.

Usually
A JavaScript object literal used as JSONYAML or an env-style file being parsed as JSONHand-written fixtures
Fix
Wrap every property name in double quotes.

The fix

Wrap every property name in double quotes. Values keep their own rules: strings quoted, numbers and booleans bare.

broken.jsonFails to parse
{
  name: "Ada",
  id: 7
}
JSON.parse stops at line 2, column 3.

Lines 2 and 3 change.

fixed.jsonParses
{
  "name": "Ada",
  "id": 7
}
JSON.parse reads it.

How the message appears

Chrome and Node first, then Firefox, then Safari. Older versions of Chrome and Node word it differently. Those lines are marked.

Console
  • SyntaxError: Expected property name or '}' in JSON at position 4 (line 2 column 3)Chrome and Node
  • SyntaxError: JSON.parse: expected property name or '}' at line 2 column 3 of the JSON dataFirefox
  • SyntaxError: JSON Parse error: Expected '}'Safari
  • SyntaxError: Expected property name or '}' in JSON at position 4Older V8 wording

What causes it

  1. A JavaScript object literal used as JSON

    JavaScript allows unquoted keys when they are valid identifiers, so {name: "Ada"} is fine in a .js file and invalid in a .json one. The two syntaxes look similar enough that this is easy to miss in review.

  2. YAML or an env-style file being parsed as JSON

    If the whole document uses key: value with no braces or quotes, it is not malformed JSON: it is a different format, and it needs a YAML parser rather than a fix.

  3. Hand-written fixtures

    Test fixtures typed by hand rather than generated. JSON.stringify always quotes keys, so anything it produces is safe.

Worth knowing

NoteJSON5 permits unquoted keys. If you control the reader and genuinely want that, use a JSON5 parser rather than hand-relaxing JSON.

NoteKeys are strings, so {"1": "a"} is legal and {1: "a"} is not, even though both work as JavaScript.

What you put into the tools never leaves your browser. We count page views and keep them for 90 days. Accept and we also remember this browser, so a return visit is not counted as new. Privacy policy