JSONDecodeError: Expecting ',' delimiter
Python’s json module raises this when it has finished reading one value and the next character isn’t the comma it needs before another one. Sometimes a comma really is missing, as in {"id": 7 "name": "Maya"}. Other times, the commas are all there and a double quote inside a string ended that string early, so Python thinks the next word is a new value. The message tells you where it stopped: line 1 column 31 (char 30) means index 30 in the string.
Quick fix
- Look at the text just before the
charposition in the message. - If two values sit next to each other with nothing between them, add the comma.
- If the spot is in the middle of a sentence, a double quote in that string needs a backslash, like
\"hi\". Building the JSON withjson.dumpsdoes that for you.
Fix your JSON
Finding the spot
In a long document, counting to the character yourself isn’t practical. JSONDecodeError has the position as e.pos, so print the text around it:
import json
text = '{"id": 7, "title": "She said "hi" to me", "tags": ["quotes"]}'
try:
data = json.loads(text)
except json.JSONDecodeError as e:
print(e.msg, "at char", e.pos)
print(repr(text[max(0, e.pos - 20):e.pos + 10]))Expecting ',' delimiter at char 30
'"title": "She said "hi" to me"'The error points at the h of hi, which is where Python expected a comma. Look at what comes just before it to see why.
A double quote inside a string
In the example above, the title is meant to be She said "hi" to me. JSON strings are wrapped in double quotes, so the quote before hi ends the string after She said . Python then expects a comma or a closing brace, finds hi, and stops.
Inside a JSON string, a double quote has to be written as \". This usually goes wrong when JSON is put together by hand or with an f-string, which drops the value in as it is. json.dumps escapes it properly:
name = 'She said "hi" to me'
# Breaks as soon as name contains a double quote
text = f'{{"id": 7, "title": "{name}"}}'
# Escapes it for you
text = json.dumps({"id": 7, "title": name})
# {"id": 7, "title": "She said \"hi\" to me"}If the broken JSON comes from somewhere else, such as an API, a log file, or a language model, fix the escaping where it’s produced if you can. Fixing the quotes afterwards means guessing which ones were supposed to end the string.
JSON that was cut off
You also get this error when the text stops right after a complete value. Python reads "Weekend walks", reaches the end, and reports that it expected a comma:
text = '{"id": 7, "title": "Weekend walks"'
json.loads(text)
# json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 35 (char 34)
len(text)
# 34When the char number equals the length of the text, nothing is missing in the middle. The JSON just ends too early, and the unexpected end of JSON input guide covers the usual reasons, like a response that was cut short or a file that was still being written.
Expecting ':' delimiter
A similar error, Expecting ':' delimiter, means a key isn’t followed by a colon, as in {"id" 7}. It’s found the same way: look just before the position in the message. If the JSON is otherwise fine, e.pos will land on the value right after the key.
The same error elsewhere
| Chrome, Edge, Node.js (object) | Expected ',' or '}' after property value in JSON at position 9 (line 1 column 10) |
|---|---|
| Chrome, Edge, Node.js (array) | Expected ',' or ']' after array element in JSON at position 5 (line 1 column 6) |
| Firefox | JSON.parse: expected ',' or '}' after property value in object at line 1 column 10 of the JSON data |
| Go | invalid character '"' after object key:value pair |
| PHP, json_last_error_msg() | Syntax error |