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

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

When programming, you may encounter JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1). What is the cause of this error, and is there any way to deal with it? Details are revealed in the article below.

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

The error occurs because you use json.loads function to parse an invalid Json string.

Example: 

import json

invalidJsonStr = "{'Name': 'John', 'Age': 18, 'Vacations':'Student'}"

# Use the json.loads() function to parse a Json string into a Python dictionary
dictObj = json.loads(invalidJsonStr)
print('Json string converted to dictionary:', dictObj)

Output:

Traceback (most recent call last):
 File "./prog.py", line 6, in <module>
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

How to solve the JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)?

Declare a valid Json string

As the cause of the error in the above example, you have declared an invalid Json string. The quotation marks you used to set your string are incorrect, so they are not accepted in the Json format.

Example:

  • All you need to do is declaring a valid Json string by placing single quotes to create the string, key-value pairs inside the string use double quotes.
import json

validJsonStr = '{"Name": "John", "Age": 18, "Vacations": "Student"}'

# Use the json.loads() function to parse a Json string into a Python dictionary
dictObj = json.loads(validJsonStr)
print('Json string converted to dictionary:', dictObj)

Output:

Json string converted to dictionary: {'Name': 'John', 'Age': 18, 'Vacations': 'Student'}

Use the json.dumps() function

If you are unsure about generating a valid Json string, you can use json.dumps function to do it.

Example:

  • I have a dictionary I want to convert to a Python string to use the Json.loads function, but I need clarification on declaring a valid Json string, so use the json.dumps function to convert the dictionary to a Json string.
  • The json.loads() function can then be used.
import json

dictObj = {'Name': 'John', 'Age': 18, 'Vacations': 'Student'}

# The json.dumps function serializes a Python object into a Json string
convertToJsonStr = json.dumps(dictObj)

# Use the json.loads() function to parse a Json string into a Python dictionary
convertToDict = json.loads(convertToJsonStr)
print(convertToDict)

Output:

{'Name': 'John', 'Age': 18, 'Vacations':'Student'}

Use ast.literal_evalph method

ast.literal eval, this is one of the aids for exploring an abstract syntax tree. This function evaluates an expression node, a string containing a Python character, or the container display.

This method, ast.literal eval, securely evaluates strings containing Python values from untrusted sources without the need to interpret the contents.

Example:

import ast

invalidJsonStr = "{'Name': 'John', 'Age': 18, 'Vacations':'Student'}"

# The ast.literal_eval can securely evaluate strings containing Python values from untrusted sources
dictObj = ast.literal_eval(invalidJsonStr)
print(dictObj)

Output:

{'Name': 'John', 'Age': 18, 'Vacations': 'Student'}

Summary

Article on solving the JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) in Python is required. If there are other ways or ideas about the article, please leave a comment. Thanks for reading!

Maybe you are interested:

Leave a Reply

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