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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# 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

incorrectly escaped quotes

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

Each double quote should be escaped with two backslashes.

# Escape double quotes in the string with two backslashes

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"'}}

escape double quotes in string with two backslashes

You only have to escape the double quotes in the contents of the string with two backslashes, not the enclosing double quotes.

main.py
import json # โœ… Don't have to escape enclosing double quotes data = json.loads( '{ "employee":{ "name":"Alice developer" } }' ) # โœ… Escape double quotes in the contents of the string data = json.loads( '{ "employee":{ "name":"Alice \\"developer\\"" } }' ) print(data) # ๐Ÿ‘‰๏ธ {'employee': {'name': 'Alice "developer"'}}

# Declaring a raw string instead

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"'}}

declare raw string

Strings that are prefixed with r are called raw strings and treat backslashes as literal characters.

Therefore, you don't have to escape the double quotes with 2 backslashes and can use a single backslash character.

main.py
import json # โœ… Declaring a raw string data = json.loads( r'{ "employee":{ "name":"Alice \"developer\"" } }' ) print(data) # ๐Ÿ‘‰๏ธ {'employee': {'name': 'Alice "developer"'}} # ------------------------------------------------------------ # โœ… Declaring a regular string data = json.loads( '{ "employee":{ "name":"Alice \\"developer\\"" } }' ) print(data) # ๐Ÿ‘‰๏ธ {'employee': {'name': 'Alice "developer"'}}

The first example uses a raw string, so we didn't have to use two backslashes to escape the double quotes in the contents of the string.

# Writing JSON manually

If you get the error when writing JSON manually, it is much easier to declare a Python dictionary or list and use the JSON.dumps method to convert the value to a JSON string.

main.py
import json my_json = json.dumps( { "employee": {"name": 'Alice "developer"'} } ) my_dict = json.loads(my_json) print(my_dict) # ๐Ÿ‘‰๏ธ {'employee': {'name': 'Alice "developer"'}} print(my_dict['employee']) # ๐Ÿ‘‰๏ธ {'name': 'Alice "developer"'}

dont write json manually

We declared a Python dictionary and passed it to the JSON.dumps() method to convert it to a string.

Note that we can use single-quoted keys or values when declaring a Python dictionary.

The value of the name key contains double quotes, so we used single quotes to wrap it.

The json.dumps() method converts a Python object to a JSON formatted string.

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

# Using colons in a JSON array causes the error

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
# โœ… Separate array elements by a comma import json data = json.loads( '["name", "Alice", "age", 30]' ) print(data) # ๐Ÿ‘‰๏ธ ['name', 'Alice', 'age', 30]

Make sure that all elements of the array are separated by a comma and that there is no trailing comma.

main.py
import json # โœ… Correct all elements separated by comma data = json.loads( '["name", "Alice", "age", 30]' ) print(data) # ๐Ÿ‘‰๏ธ ['name', 'Alice', 'age', 30] # ------------------------------------------------ # โ›”๏ธ Error has trailing comma after last element data = json.loads( '["name", "Alice", "age", 30,]' )

The first example separates all elements in the JSON array by a comma, so everything works as expected.

The second example has a trailing comma after the last array element which causes the error.

# Declaring a JSON object that stores key-value pairs

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

main.py
# โœ… Declaring an object 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.

# Getting invalid JSON data from your server

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)

Notice that all objects in the array are separated by a comma and there is no trailing coma.

The keys and values of each object are separated by a colon and the key-value pairs are separated by a comma.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev