How to Solve Python JSONDecodeError: Expecting ‘,’ delimiter: line 1

by | Programming, Python, Tips

The error occurs when you try to parse an invalid JSON string to the json.loads() method call. You can solve this error by ensuring you escape double quotes with double backslashes. For example,

import json

data = json.loads(
   '{ "particle":{ "name":"electron \\"lepton\\"" } }'
)
print(data)

Alternatively, you can use r to indicate the string is a raw string. Otherwise, you have to correct any other errors in the JSON string,


JSONDecodeError: Expecting ‘,’ delimiter: line 1

In Python, JSONDecodeError occurs when there is an issue with the formatting of the JSON data. This specific error tells us the JSON decoder has encountered an invalid JSON string.

Example #1: Not Escaping Double Quotes

Let’s look at an example of an invalid JSON string.

import json

data = json.loads(
   '{ "particle":{ "name":"electron "lepton"" } }'
)

print(data)

In the above example, the value of the name key contains double quotes. Let’s run the code to see what happens:

JSONDecodeError: Expecting ',' delimiter: line 1 column 34 (char 33)

The error occurs because we did not escape the double quotes "" in the string.

Escaping a string means to reduce the ambiguity in quotes and other characters in the string.

We need to escape the double quotes because the Python interpreter does not know where the string ends.

Solution #1: Use Double backslash to escape double quotes

We can solve the error by escaping the double quotes in the string using two backslashes \\. With this change, the Python interpreter knows that the double quotes are part of the values of the string.

Let’s look at the revised code:

import json

data = json.loads(
   '{ "particle":{ "name":"electron \\"lepton\\"" } }'
)
print(data)
print(type(data))

Let’s run the code to get the result:

{'particle': {'name': 'electron "lepton"'}}
<class 'dict'>

We successfully parsed the JSON string and converted it to a Python dictionary.

Solution #2: Use r to indicate a raw string

We can also put an r before the JSON string and add an escape the double quotes around lepton with single backslashes. Let’s look at the revised code:

import json

data = json.loads(
   r'{ "particle":{ "name":"electron \"lepton\"" } }'
)
print(data)
print(type(data))

Let’s run the code to get the result:

{'particle': {'name': 'electron "lepton"'}}
<class 'dict'>

We successfully parsed the JSON string and converted it to a Python dictionary.

Example #2: Incorrectly Declaring an Array

Let’s look at another example of an invalid JSON string.

import json

data = json.loads(
'["name":"electron", "charge": -1, "mass":0.511]'
)
print(data)

In the above example, we have a string that contains an array. Let’s run the code to see what happens:

JSONDecodeError: Expecting ',' delimiter: line 1 column 8 (char 7)

The error occurs because the JSON string has : characters in the array, which is invalid syntax for the JSON decoder.

Solution #1: Use commas instead of colons

If we want to create a valid JSON array, we have to separate elements with commas not colons. Let’s look at the revised code:

import json

data = json.loads(
'["name","electron", "charge", -1, "mass",0.511]'
)
print(data)
print(type(data))

Let’s run the code to get the result:

['name', 'electron', 'charge', -1, 'mass', 0.511]
<class 'list'>

We successfully parsed a valid JSON string containing an array and returned a Python list.

Solution #2: Wrap in Curly Braces for Key-Value Pairs

If we want to create a set of key-value pairs, we need to keep the colons separating the keys and values wrap the set in curly braces. Let’s look at the revised code:

import json

data = json.loads(
'{"name":"electron", "charge": -1, "mass":0.511}'
)
print(data)
print(type(data))

Let’s run the code to get the result:

{'name': 'electron', 'charge': -1, 'mass': 0.511}
<class 'dict'>

We successfully parsed a valid JSON string containing an set of key-value pairs and returned a Python dictionary.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on errors involving JSON, go to the articles:

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!