Unquoted property names in JSON
SyntaxError: Expected property name or '}' in JSON at position 4 (line 2 column 3)
Where it stops
{
name: "Ada",
id: 7What 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.
{
name: "Ada",
id: 7
}Lines 2 and 3 change.
{
"name": "Ada",
"id": 7
}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.
- 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
A JavaScript object literal used as JSON
JavaScript allows unquoted keys when they are valid identifiers, so
{name: "Ada"}is fine in a.jsfile and invalid in a.jsonone. The two syntaxes look similar enough that this is easy to miss in review.YAML or an env-style file being parsed as JSON
If the whole document uses
key: valuewith no braces or quotes, it is not malformed JSON: it is a different format, and it needs a YAML parser rather than a fix.Hand-written fixtures
Test fixtures typed by hand rather than generated.
JSON.stringifyalways 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.