SyntaxError: Expected property name or '}' in JSON

After { or a comma inside an object, JSON requires a key written as a double-quoted string. Anything else at that spot, such as a bare word, a single quote, a number or a comment, produces this error. It usually means the text is a JavaScript object literal or a printed Python dict rather than JSON. Python reports the same problem as “Expecting property name enclosed in double quotes”.

Seen as:

  • SyntaxError: Expected property name or '}' in JSON at position 4 (line 2 column 3)
  • SyntaxError: Unexpected token n in JSON at position 4
  • SyntaxError: JSON.parse: expected property name or '}' at line 2 column 3 of the JSON data
  • json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 3 (char 4)
  • invalid character 'n' looking for beginning of object key string

Input

Settings

History

Load from URL

Common causes

1. Unquoted keys from a JavaScript object literal

JavaScript lets you write { name: "Ada" }; JSON does not. Every key must be in double quotes. If the text came from console.log or from source code, produce it with JSON.stringify(value, null, 2) instead, because the console prints a display format and not JSON.

Before
{ name: "Ada Lovelace", email: "[email protected]" }
After
{ "name": "Ada Lovelace", "email": "[email protected]" }

2. Keys in single quotes

Single-quoted keys are valid in JavaScript and Python but not in JSON. Replace both quotes of every key and string value with double quotes. The usual source is str(my_dict) in Python, which prints a dict in Python syntax; call json.dumps(my_dict) instead.

Before
{'name': 'Ada Lovelace', 'admin': true}
After
{"name": "Ada Lovelace", "admin": true}

3. A comment where a key should be

A // or /* comment at the start of a line inside an object is read as the start of a key. JSON has no comments; remove them or parse the file as JSONC. A comment placed straight after a comma gets the “double-quoted property name” wording instead.

Before
{
  // primary contact
  "email": "[email protected]"
}
After
{
  "email": "[email protected]"
}

4. A number used as a key

JavaScript object literals and Python dicts allow numeric keys, but a JSON key is always a string. Quote the number. Both JSON.stringify and json.dumps convert numeric keys to strings for you, so this mostly appears in hand-written or printed output.

Before
{1: "Ada", 2: "Grace"}
After
{"1": "Ada", "2": "Grace"}

5. A comma after the last property (newer wording)

Current Chrome and Node phrase this case as “Expected double-quoted property name”. The comma promises another key, so the parser finds } instead. See trailing commas in JSON.

Before
{"email": "[email protected]", "admin": true,}
After
{"email": "[email protected]", "admin": true}

The same mistake in other parsers

Given a bare key name on line 2, each parser words the problem differently, and only some of them give a position:

  • Node 22 and later, current Chrome: Expected property name or '}' in JSON at position 4 (line 2 column 3)
  • Node 20: the same text without the line and column
  • Node 18 and earlier: Unexpected token n in JSON at position 4
  • Firefox: JSON.parse: expected property name or '}' at line 2 column 3 of the JSON data
  • Python: Expecting property name enclosed in double quotes: line 2 column 3 (char 4)
  • Go encoding/json: invalid character 'n' looking for beginning of object key string
  • Jackson (Java): Unexpected character ('n' (code 110)): was expecting double-quote to start field name
  • System.Text.Json (.NET): 'n' is an invalid start of a property name. Expected a '"'.

Go’s message carries no location. Assert the error to *json.SyntaxError and read its Offset field to get one.

Newtonsoft.Json is the odd one out: it accepts unquoted keys and single quotes without complaint. A payload can therefore pass through a .NET service and then fail in every other consumer downstream.

Finding the bad key in a large or minified file

The position is a zero-based offset into the text. On minified JSON the line number is always 1, so print the characters around the offset instead:

try {
  JSON.parse(text);
} catch (err) {
  const at = Number(/position (\d+)/.exec(err.message)?.[1]);
  console.log(text.slice(Math.max(0, at - 40), at + 40));
}
try:
    json.loads(text)
except json.JSONDecodeError as err:
    print(err.lineno, err.colno, repr(err.doc[max(0, err.pos - 40):err.pos + 40]))

Offsets are not byte counts. JavaScript counts UTF-16 code units and Python counts code points, so the two disagree once the text contains an emoji. From a terminal, python -m json.tool file.json prints the same line and column without any code.

A parser stops at the first bad key. If the whole document uses object-literal syntax, expect the error to move down one key each time you fix one by hand.

Converting object-literal text without breaking it

A regular expression such as /(\w+):/g looks like a quick fix, but it also rewrites text inside strings: the value "https://example.com" contains https:. Passing the text to eval is worse, because it runs whatever the text contains. Use a parser built for the source format:

  • JavaScript or JSON5 text: JSON5.parse from the json5 package, followed by JSON.stringify.
  • A printed Python dict: ast.literal_eval(text) followed by json.dumps. It understands single quotes, True and None, and it does not execute code.
  • Mixed damage: PasteKit’s Fix it button and the JSON Repair page quote keys, swap single quotes and drop comments and trailing commas. Fix it lists each change before you accept it.

To stop the error coming back, have the program that writes this text call a serialiser. Hand-built strings are where unquoted keys come from.

Frequently asked questions

Is this the same error as "Expecting property name enclosed in double quotes"?

Yes. That is Python’s wording for the same problem; the causes and fixes on this page apply to both, including text printed from a Python dict.

Why did older Node versions say "Unexpected token n"?

Node 18 and earlier reported the first character of the bad key, so an unquoted name key gave “Unexpected token n in JSON at position N”. Node 20 moved to describing what was expected, and Node 22 added the line and column.

What is the difference between "Expected property name or '}'" and "Expected double-quoted property name"?

The first appears straight after an opening brace, where a closing brace would also be legal. The second appears after a comma, where only a key can follow, which is why trailing commas produce it.

Why does my .NET service accept this JSON when the browser does not?

Newtonsoft.Json tolerates unquoted keys, single quotes and trailing commas by default, while JSON.parse follows the standard strictly. System.Text.Json is strict as well, so moving between the two .NET libraries can expose this error.

Can I keep unquoted keys?

Only if you switch format. JSON5 allows unquoted keys, single quotes and comments, and PasteKit can convert JSON5 to strict JSON in one step.

Related