ENRU

Expecting property name enclosed in double quotes (Python)

Updated

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) means Python’s json was inside an object, right after { or a comma, and the next character was not ". JSON keys wear double quotes. This one didn’t, or there was no key at all. Most often the text isn’t JSON but a Python dict printed with str().

What sits at that position

Look at the character at char N, counting from 0. It is one of five things:

TextPython 3.9.6Python 3.14.10Cause
{'id': 42}Expecting property name enclosed in double quotes: line 1 column 2 (char 1)the sameSingle quotes, usually from str(dict): single quotes
{"id": 42,}Expecting property name enclosed in double quotes: line 1 column 11 (char 10)Illegal trailing comma before end of object: line 1 column 10 (char 9)Trailing comma
{, then // user id on the next lineExpecting property name enclosed in double quotes: line 2 column 3 (char 4)the sameComment
{id: 42}Expecting property name enclosed in double quotes: line 1 column 2 (char 1)the sameUnquoted key
{“id”: 42}Expecting property name enclosed in double quotes: line 1 column 2 (char 1)the sameSmart quotes from a word processor or a chat

Python 3.13 changed one case. A trailing comma now has its own message and points at the comma itself, one character earlier; Python 3.12 and older report Expecting property name at the } that follows. Compare Lib/json/decoder.py in 3.12 and 3.13.

RFC 8259 is explicit: “A name is a string”, and a string “begins and ends with quotation marks”, the " character (%x22 in its grammar). Single quotes, bare words and comments don’t qualify, and after a comma another member has to follow.

The usual suspect: str(dict)

A dict printed with str(), print(), an f-string or %s logging looks like JSON and isn’t: single quotes, True, None. json.dumps writes the real thing:

import json

payload = {"id": 42, "ok": True, "tags": None}
print(str(payload))         # {'id': 42, 'ok': True, 'tags': None}
print(json.dumps(payload))  # {"id": 42, "ok": true, "tags": null}

Where a dict gets turned into text by mistake:

  • requests.post(url, data=str(payload)). Send json=payload instead: requests serializes it and sets Content-Type: application/json.
  • f.write(str(data)) instead of json.dump(data, f).
  • A message queue, a cache or a database column filled with str(obj).
  • Another program gluing strings together with single quotes. Every language has a serializer; see the table below.
  • Windows cmd.exe, where single quotes don’t quote: curl -d '{"id": 42}' arrives mangled. Put the body in a file and send it with -d @body.json.

Don’t repair the text with .replace("'", '"'). It breaks on any apostrophe inside a value and leaves True and None behind; on the payload above the next error is Expecting value: line 1 column 18 (char 17). If the producer really is Python and you can’t change it, read its output as a Python literal, not as JSON:

import ast

data = ast.literal_eval(text)

ast.literal_eval evaluates literals and nothing else (dicts, lists, strings, numbers, True, None), but it is meant for Python output you trust, not for arbitrary input.

Producing real JSON in other languages

When the text comes from another program, fix it there. Every call in the middle column was run and returns {"id":42}; Python adds a space after the colon.

LanguageDo thisNot this
JavaScriptJSON.stringify({ id: 42 })"{'id': " + id + "}"
Pythonjson.dumps({"id": 42})str({"id": 42})
Gojson.Marshal(map[string]any{"id": 42})fmt.Sprintf("{'id': %d}", id)
Java (Jackson)mapper.writeValueAsString(Map.of("id", 42))"{'id': " + id + "}"
C#JsonSerializer.Serialize(new { id = 42 })$"{{'id': {id}}}"
PHPjson_encode(['id' => 42])"{'id': $id}"

Comments and trailing commas in hand-written files

Configuration files edited by people collect // comments and trailing commas. Both are allowed in JSONC and JSON5; both are rejected by json. Remove them, or read the file with a parser for the format it is really in:

import json5  # pip install json5

data = json5.loads(text)

Checked with json5 0.17.3: comments, trailing commas, unquoted keys and single quotes all load. The trade-offs are in comments in JSON.

Find the character

The exception carries the position as numbers. Print the line with a caret under the column:

try:
    data = json.loads(text)
except json.JSONDecodeError as e:
    line = text.splitlines()[e.lineno - 1]
    print(f"{e.msg} at line {e.lineno}, column {e.colno}")
    print(line)
    print(" " * (e.colno - 1) + "^")

For a comment on the third line this prints Expecting property name enclosed in double quotes at line 3, column 3, then the line // owner, then a caret under the first slash.

The same input in other parsers

{'id': 42} fed to every parser available for the check. Two of them accept it, which is how such text survives until it reaches Python:

Parser, version checkedMessageNote
Chrome 156, Node.js 25.1 (V8)SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)Node.js 20 prints it without the line and column
Node.js 18.20 (V8 10.2)SyntaxError: Unexpected token ' in JSON at position 1The older wording
Firefox (source: SpiderMonkey)SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data
Safari, JavaScriptCore of macOS 27.0.1SyntaxError: JSON Parse error: Single quotes (') are not allowed in JSONThe only one that names the problem
Go 1.25.3invalid character '\'' looking for beginning of object key string
Java, Jackson 2.20.1Unexpected character (''' (code 39)): was expecting double-quote to start field name
Java, Gson 2.13.2no errorfromJson reads single quotes leniently
C#, System.Text.Json 10 (runtime 10.0.14)''' is an invalid start of a property name. Expected a '"'.Followed by LineNumber: 0 | BytePositionInLine: 1.
C#, Newtonsoft.Json 13.0.4no errorAccepts single quotes and trailing commas
PHP 8.5.12Syntax errorCode 4
jq 1.7.1jq: parse error: Invalid numeric literal at line 1, column 6
jq 1.8.2jq: parse error: Invalid string literal; expected ", but got ' at line 1, column 6
FastAPI 0.128.8, as the server422 with "msg":"JSON decode error" and "error":"Expecting property name enclosed in double quotes"The Python message reaches the client inside the response
Flask 3.1.3, as the server400 Bad Request as an HTML pageThe client then fails with Unexpected token '<'

Try it

Open the validator with a single-quoted or a trailing-comma sample to see how each is reported: the error names the character and gives its line and column.

Related: Expecting value, comments in JSON, and the list of JSON errors.

Expecting property name FAQ

What does “Expecting property name enclosed in double quotes” mean?

Inside an object, after { or a comma, Python expected a key in double quotes and found something else: a single quote, the closing brace after a trailing comma, a comment or a bare word.

How do I convert a Python dict to JSON?

json.dumps(d) for a string, json.dump(d, f) for a file. Never str(d): it writes single quotes, True and None.

Can Python’s json accept single quotes?

No, and there is no option for it. Fix the producer. If the text is Python output you trust, use ast.literal_eval; for a hand-written config, a JSON5 parser.

Why does the same file fail differently on Python 3.13?

Python 3.13 reports a trailing comma as Illegal trailing comma before end of object and points at the comma; older versions report Expecting property name at the brace after it.