Last updated: Apr 8, 2024
Reading timeยท4 min
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.
Here is an example of how the error occurs.
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.
import json data = json.loads( '{ "employee":{ "name":"Alice \\"developer\\"" } }' ) print(data) # ๐๏ธ {'employee': {'name': 'Alice "developer"'}}
You only have to escape the double quotes in the contents of the string with two backslashes, not the enclosing double quotes.
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"'}}
Alternatively, you can prefix the string with r
to indicate that it's a raw
string.
import json data = json.loads( r'{ "employee":{ "name":"Alice \"developer\"" } }' ) print(data) # ๐๏ธ {'employee': {'name': 'Alice "developer"'}}
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.
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.
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.
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"'}
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.
Here is an example of a syntax error due to using colons in a JSON array.
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.
# โ 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.
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.
If you meant to declare a set of key-value pairs, wrap them in curly braces.
# โ 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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: