json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1

avatar

Borislav Hadzhiev

Last updated: Apr 24, 2022

banner

Photo from Unsplash

json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 #

The Python "json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1" occurs when we try to parse an invalid JSON string. To solve the error, make sure to escape any double quotes with two backslashes and correct any other errors in the JSON string.

typeerror load missing 1 required positional argument loader

Here is an example of how the error occurs.

main.py
import json # ⛔️ json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 31 (char 30) data = json.loads( '{ "employee":{ "name":"Alice \"developer\"" } }' ) # 👆️ did not escape JSON str correctly

Notice that the value of the name key contains double quotes.

If you have double quotes in the string, make sure to escape them using two backslashes.

main.py
import json data = json.loads( '{ "employee":{ "name":"Alice \\"developer\\"" } }' ) print(data) # 👉️ {'employee': {'name': 'Alice "developer"'}}

Alternatively, you can prefix the string with r to indicate that it's a raw string.

main.py
import json data = json.loads( r'{ "employee":{ "name":"Alice \"developer\"" } }' ) print(data) # 👉️ {'employee': {'name': 'Alice "developer"'}}

The error is also caused if we have any syntax errors in the JSON string.

Here is an example of a syntax error due to using colons in a JSON array.

main.py
import json # ⛔️ json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 8 (char 7) data = json.loads( '["name": "Alice", "age": 30]' )

If you meant to declare a JSON array, make sure to separate its elements with a comma.

main.py
import json data = json.loads( '["name", "Alice", "age", 30]' ) print(data) # 👉️ ['name', 'Alice', 'age', 30]

If you meant to declare a set of key-value pairs, wrap them in curly braces.

main.py
import json data = json.loads( '{"name": "Alice", "age": 30}' ) print(data) # 👉️ {'name': 'Alice', 'age': 30}

The fastest way to validate and correct your JSON is to use a JSON validator.

Paste your payload into the form, the validator checks for errors and sometimes directly fixes them.

If you are making an HTTP request and getting invalid JSON data, you have to fix the issue on the backend.

Here is an example of a more complex JSON string.

main.py
import json data = json.loads( """ [ {"id": 1, "name": "Alice", "is_subscribed": true}, {"id": 2, "name": "Bob", "is_subscribed": false}, {"id": 3, "name": "Carl", "is_subscribed": true, "address": {"country": "Canada"}} ] """ ) # 👇️ [{'id': 1, 'name': 'Alice', 'is_subscribed': True}, {'id': 2, 'name': 'Bob', 'is_subscribed': False}, {'id': 3, 'name': 'Carl', 'is_subscribed': True, 'address': {'country': 'Canada'}}] print(data)
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.