JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

The Python "JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" occurs when we try to parse an invalid JSON string (e.g. single-quoted keys or values, or a trailing comma).

Use the ast.literal_eval() method to solve the error.

Here is an example of how the error occurs.

main.py
import json invalid_json = r"{'name': 'Bobby'}" # ๐Ÿ‘ˆ๏ธ single-quoted # โ›”๏ธ json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) result = json.loads(invalid_json)

single quoted property

Notice that the string has a single-quoted key and value, which means that it isn't a valid JSON string.

Rather, it's just a Python dictionary that's wrapped in a string.

JSON (string) keys and values must be enclosed in double quotes.
main.py
import json # โœ… Valid JSON string valid_json = r'{"name": "Bobby Hadz", "age": 30}' # ๐Ÿ‘ˆ๏ธ no trailing comma a_dict = json.loads(valid_json) print(a_dict) # {'name': 'Bobby Hadz', 'age': 30}

valid json string

The example string above is valid JSON because the properties and values are double-quoted and there is no trailing comma.

# Using the ast.literal_eval method to evaluate the string

You can solve the error by using the ast.literal_eval method to evaluate the string that contains a Python literal.

main.py
import ast invalid_json = r"{'name': 'Bobby Hadz'}" result = ast.literal_eval(invalid_json) print(type(result)) # ๐Ÿ‘‰๏ธ <class 'dict'> print(result) # ๐Ÿ‘‰๏ธ {'name': 'Bobby Hadz'} print(result['name']) # ๐Ÿ‘‰๏ธ Bobby Hadz

using ast literal eval to evaluate the string

The ast.literal_eval() method allows us to safely evaluate a string that contains a Python literal.

The string may consist of strings, bytes, numbers, tuples, lists, dicts, sets, booleans and None.

# Converting a Python dictionary to JSON

If you are trying to convert a Python dictionary to a JSON string, use the json.dumps() method.

main.py
import json my_dict = { 'id': 1, 'name': 'Bobby Hadz', 'age': 30, } json_str = json.dumps(my_dict) print(json_str) # {"id": 1, "name": "Bobby Hadz", "age": 30} print(type(json_str)) # <class 'str'>

convert python dictionary to json

Notice that we didn't wrap the my_dict variable in single or double quotes to declare a dictionary.

If you wrap the variable in quotes, you end up declaring a string.

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

# Using the str.replace method to replace single with double quotes

An alternative approach is to use the str.replace() method to replace all single quotes in the string with double quotes.

main.py
import json invalid_json = r"{'name': 'Alice'}" valid_json = invalid_json.replace("\'", "\"") print(valid_json) # ๐Ÿ‘‰๏ธ '{"name": "Alice"}' my_dict = json.loads(valid_json) print(my_dict) # ๐Ÿ‘‰๏ธ {'name': 'Alice'} print(type(my_dict)) # ๐Ÿ‘‰๏ธ <class 'dict'>

replace single with double quotes

The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)
main.py
a_str = "'one', 'two', 'three'" result = a_str.replace("'", '"') print(result) # "one", "two", "three"
However, this is very risky because the string could contain a single quote somewhere in its content, e.g. It's her.

# Using ast.literal_eval to deal with trailing commas

You can also use the ast.literal_eval method if your string contains a trailing comma.

main.py
import ast invalid_json = r'{"name": "Bobby Hadz",}' # ๐Ÿ‘ˆ๏ธ has trailing comma result = ast.literal_eval(invalid_json) print(type(result)) # ๐Ÿ‘‰๏ธ <class 'dict'> print(result) # ๐Ÿ‘‰๏ธ {'name': 'Bobby Hadz'} print(result['name']) # ๐Ÿ‘‰๏ธ Bobby Hadz

The trailing comma also makes the string invalid JSON, but Python dictionaries are allowed to have a trailing comma after the last key-value pair.

# Using the PyYAML module to parse the string

Alternatively, you can try using the yaml.safe_load() method.

Make sure you have pyyaml installed by running pip install pyyaml.

shell
pip install pyyaml pip3 install pyyaml

Now you can import and use the module to parse the JSON string.

main.py
import yaml invalid_json = r'{"name": "Alice",}' # ๐Ÿ‘ˆ๏ธ has trailing comma my_dict = yaml.safe_load(invalid_json) print(my_dict) # ๐Ÿ‘‰๏ธ {'name': 'Alice'} print(type(my_dict)) # ๐Ÿ‘‰๏ธ <class 'dict'>

Make sure the value you are trying to parse is a JSON string and has not been already parsed into a native Python object.

# Using str.replace() method to remove trailing commas

You can also use the str.replace() method to remove the trailing commas from a string.

main.py
import json invalid_json = r'{"name": "Bobby Hadz",}' # ๐Ÿ‘ˆ๏ธ has trailing comma valid_json = invalid_json.replace( ',}', '}').replace(',]', ']').replace(',)', ')') print(valid_json) # {"name": "Bobby Hadz"} a_dict = json.loads(valid_json) print(a_dict) # {'name': 'Bobby Hadz'}

We chained 3 calls to the str.replace() method.

The first call removes trailing commas right before a curly brace.

The second call removes trailing commas right before a square bracket.

And the third call removes trailing commas right before a parenthesis.

# Valid JSON vs invalid JSON

Here are some examples of valid and invalid JSON strings.

main.py
# โ›”๏ธ invalid JSON (has single-quoted keys and values) invalid_json = r"{'name': 'Alice', 'age': 30, 'tasks' ['a', 'b', 'c']}" # โœ… valid JSON (double-quoted keys and values) valid_json = r'{"name": "Alice", "age": 30, "tasks": ["a", "b", "c"]}'

Make sure you don't have a trailing comma after the last element.

main.py
# โ›”๏ธ invalid JSON (has a trailing comma after the last key-value pair) invalid_json = r'{"name": "Alice", "age": 30,}' # โœ… valid JSON (has no trailing comma after the last key-value pair) valid_json = r'{"name": "Alice", "age": 30}'

# Converting a Python object to a JSON string and vice versa

If you need to parse a JSON string into a native Python object, you have to use the json.loads() method, and if you need to convert a Python object into a JSON string, you have to use the json.dumps() method.

main.py
import json json_str = r'{"name": "Alice", "age": 30}' # โœ… parse JSON string to Python native dict my_dict = json.loads(json_str) print(type(my_dict)) # ๐Ÿ‘‰๏ธ <class 'dict'> # โœ… convert Python native dict to a JSON string my_json_str = json.dumps(my_dict) print(type(my_json_str)) # ๐Ÿ‘‰๏ธ <class 'str'>

The json.loads() method basically helps us load a Python native object (e.g. a dictionary or a list) from a JSON string.

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

The JSONEncoder class supports the following objects and types by default.

PythonJSON
dictobject
list, tuplearray
strstring
int, float, int and float derived Enumsnumber
Truetrue
Falsefalse
Nonenull

# Validating your JSON strings

You can paste your code into an online Syntax Validator . The validator should be able to tell you on which line the error occurred.

You can hover over the squiggly red line to get additional information on why the error was thrown.

# Checking what type a variable stores

If you aren't sure what type of object a variable stores, use the built-in type() class.

main.py
my_dict = {'name': 'Alice', 'age': 30} print(type(my_dict)) # ๐Ÿ‘‰๏ธ <class 'dict'> print(isinstance(my_dict, dict)) # ๐Ÿ‘‰๏ธ True my_str = 'hello world' print(type(my_str)) # ๐Ÿ‘‰๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐Ÿ‘‰๏ธ True

The type class returns the type of an object.

The isinstance() function returns True if the passed-in object is an instance or a subclass of the passed-in class.

# 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