JSONDecodeError: Expecting ',' delimiter: How to Fix It in Python
Python raises JSONDecodeError: Expecting ',' delimiter when json.loads() finds something other than a comma or closing bracket after a value. Learn the…
Have broken JSON right now? Fix it free in under 1 second — no signup.
Fix My JSON →Python raises this error when json.loads() finishes a value and then finds a character that is not a comma or a closing bracket. The error message gives a line, a column and a character position, and the real mistake usually sits just before that spot.
What “JSONDecodeError: Expecting ',' delimiter” means
The parser finished reading a value inside an object or array and expected a comma or a closing brace or bracket next. RFC 8259 requires a comma between members and between array elements. Python's json module reports the position where it gave up, such as line 3 column 3. That position is where the unexpected character starts. The actual gap is usually at the end of the previous line or value.
Causes and the fix for each
A comma is missing between two object members.
Each member in an object must be separated from the next by a comma. When a line ends without one, the parser sees the next key where it wanted a comma. This is the most common version of the error in hand-edited files.
Before (fails):{"name": "Ada" "age": 36}
After (valid):
{"name": "Ada", "age": 36}
Two array values have no comma between them.
Two values sit side by side with only whitespace between them. The parser reads the first value, then meets the second one where a comma or closing bracket should be. Lists copied from a spreadsheet, a log or a text file often look like this, because spaces or line breaks were used as separators.
Before (fails):{"ids": [1 2 3]}
After (valid):
{"ids": [1, 2, 3]}
A double quote inside a string is not escaped.
The string ends at the first inner quote. The text after it is then read as a new token, and the parser expects a comma. Escape the inner quote with a backslash.
Before (fails):{"quote": "She said "hi" to me"}
After (valid):
{"quote": "She said \"hi\" to me"}
A bracket is closed with the wrong character or left open.
An array that is closed with a brace, or never closed, makes the parser look for another element. It sees the wrong closing character and reports a missing comma.
Before (fails):{"a": [1, 2}
After (valid):
{"a": [1, 2]}
A wrong separator such as a semicolon is used.
Only a comma separates members in JSON. A semicolon, a pipe or a newline alone is read as an unexpected character after the value.
Before (fails):{"a": 1; "b": 2}
After (valid):
{"a": 1, "b": 2}
How to fix it in Python
- Read the error text. It ends with line, column and char numbers, for example line 3 column 3 (char 24). Open the input and go to that spot.
- Look at the end of the value just before that position. Check for a missing comma, a stray quote or a wrong closing bracket.
- If the position points into the middle of a string, look for an unescaped double quote earlier in that string and add a backslash before it.
- Print a short slice of the input around the char position with s[pos-20:pos+20]. This shows the text the parser saw without scrolling a large file.
- Paste the text into the Validator to see the first error, or into the Repair tool to fix missing commas and quotes automatically. Use the Formatter afterwards to check the structure by eye.
- If the text comes from an API or a model, log the raw string before parsing. Fix the source if the same mistake repeats.
import json
def load_json(text):
try:
return json.loads(text)
except json.JSONDecodeError as e:
start = max(e.pos - 20, 0)
snippet = text[start:e.pos + 20]
raise ValueError(
f"Bad JSON at line {e.lineno}, column {e.colno}: {e.msg}. "
f"Near: {snippet!r}"
) from e
Variants of the message
| Message | Cause | Fix |
|---|---|---|
| Expecting ',' delimiter | Missing comma, unescaped quote or wrong closing bracket after a value | Add the comma, escape the quote or fix the bracket just before the reported position |
| Expecting property name enclosed in double quotes | Trailing comma before a closing brace, or single-quoted keys | Remove the trailing comma and use double quotes for keys |
| Expecting value | A value is missing, such as a trailing comma in an array or an empty input | Remove the extra comma or supply the value |
| Expecting ':' delimiter | A key is not followed by a colon | Add the colon between key and value |
| Unterminated string starting at | A string has no closing double quote | Add the closing quote or escape inner quotes |
| Extra data | Valid JSON is followed by more text, such as two documents in one string | Split the documents or remove the trailing text |
Related errors
- Fix Python JSON Errors: json.loads() Decode Errors and How to Repair Them
- Invalid JSON Meaning: What It Is and How to Fix It
- Single quotes are not valid JSON: Fixing json.loads() in Python
- Invalid control character in JSON: How to Fix It in Python
- Missing bracket in JSON: How to Fix It in JavaScript
Key takeaways
- The error means the parser expected a comma or closing bracket and found something else.
- The real mistake is usually at the end of the value before the reported position.
- Common causes are a missing comma, an unescaped inner quote and a wrong closing bracket.
- Catch JSONDecodeError and use lineno, colno and pos to show the failing spot.
- A repair tool can fix simple comma and quote mistakes in pasted text.
FAQ
Why does Python point to a different place than the real mistake?
The parser reports where it noticed the problem, not where the mistake was made. A missing comma is reported at the start of the next token, so the gap is at the end of the previous value.
Does a trailing comma cause this error?
No. A trailing comma before a closing brace or bracket raises a different message, such as Expecting property name enclosed in double quotes or Expecting value.
Can json.loads() skip the bad part and keep going?
No. The standard json module stops at the first syntax error and raises JSONDecodeError. It has no recovery mode.
Why does it happen with text from an LLM?
Model output can drop a comma, leave an inner quote unescaped or stop before a bracket closes. The parser then reports this error. A repair step or a retry can handle it.
How do I get the line and column in code?
Catch json.JSONDecodeError and read its lineno, colno and pos attributes. The msg attribute holds the message without the position.
Still dealing with broken JSON?
Paste it in and get it fixed in under 1 second — free, no signup, no install. Works with ChatGPT, Claude, n8n, and any AI output.
Fix My JSON Free →Related Articles