“Json.decoder.JSONDecodeError: Expecting ‘,’ delimiter: line 1” in Python – How to fix it?

"json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1" in python

If you are getting trouble with the error “Json.decoder.JSONDecodeError: Expecting ‘,’ delimiter: line 1” in Python, keep reading our article. We will give you some examples of common user mistakes and how to fix them, such as using double backslashes (\\) or the “r” prefix and passing dictionary strings.

Reason for the error “Json.decoder.JSONDecodeError: Expecting ‘,’ delimiter: line 1” in Python

When you work with JSON format, you will get the error when passing an invalid format string to convert to JSON format.

Mistake when determining raw strings

import json

player = json.loads(
    '{"name": "Cristiano Ronaldo "4 Golden Boot" ", "age": 37, "nationality": "Portugal"}'
)

print(player)

Result:

JSONDecodeError Traceback (most recent call last)
 in <module>
----> 4 '{"name": "Cristiano Ronaldo "4 Golden Boot" ", "age": 37, "nationality": "Portugal"}'
JSONDecodeError: Expecting ',' delimiter: line 1 column 30 (char 29)

Mistake when using the wrong brackets

import json
 
player = json.loads(
    '["name": "Cristiano Ronaldo", "age": 37, "nationality": "Portugal"]'
)
 
print(player)

Result:

JSONDecodeError Traceback (most recent call last)
 in <module>
----> 4 '["name": "Cristiano Ronaldo", "age": 37, "nationality": "Portugal"]'
JSONDecodeError: Expecting ',' delimiter: line 1 column 8 (char 7)

Solution to this problem

Using double backslash (\\) or prefix “r”

To mention a raw string, you must use the double backslash (\\) before the string. If not, the system can not understand your purpose and consider the string invalid, and you will get this error. Let’s use the double backslash to fix this error.

Code:

import json

player = json.loads(
    # Use the double backslash to determine the raw string
    '{"name": "Cristiano Ronaldo \\"4 Golden Boot\\"", "age": 37, "nationality": "Portugal"}'
)

print(player)

Result:

{'name': 'Cristiano Ronaldo "4 Golden Boot"', 'age': 37, 'nationality': 'Portugal'}

Also, you can use the prefix “r” and a single backslash to determine the raw string.

Code:

import json

player = json.loads(
    # Use the prefix "r" and a single backslash
    r' {"name": "Cristiano Ronaldo \"4 Golden Boot\"", "age": 37, "nationality": "Portugal"}'
)

print(player)

Result:

{'name': 'Cristiano Ronaldo "4 Golden Boot"', 'age': 37, 'nationality': 'Portugal'}

Passing dictionary-string 

People often make a mistake when using the format string with a square bracket, while the correct syntax is a curly bracket.

Code:

import json

player = json.loads(
    '{"name": "Cristiano Ronaldo", "age": 37, "nationality": "Portugal"}'
)

print(player)

Result:

{'name': 'Cristiano Ronaldo', 'age': 37, 'nationality': 'Portugal'}

If you want to create multiple data with a format string looking like a list of dictionary

Code:

import json
 
player = json.loads(
    """ [{"name": "Cristiano Ronaldo", "age": 37, "nationality": "Portugal"},
    {"name": "Lionel Messi", "age": 35, "nationality": "Argentina"}]"""
)
 
print(player)

Result:

[{'name': 'Cristiano Ronaldo', 'age': 37, 'nationality': 'Portugal'}, {'name': 'Lionel Messi', 'age': 35, 'nationality': 'Argentina'}]

Summary

In summary, the error “json.decoder.JSONDecodeError: Expecting ‘,’ delimiter: line 1” in Python occurs when you use an invalid string to create JSON data. To solve this error, remember to use the correct syntax, as we mentioned above. 

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *