JSON Error while converting json into python directory
Question:
I am trying to convert JSON data into Python dictionary using the following code:
import json
handle = open("json_input.json", "r")
content = handle.read()
d = json.loads(content)
print(d)
But keep getting the following error:
File "C:UsersmePycharmProjectspythonProject1main.py", line 7, in <module>
d = json.loads(content)
^^^^^^^^^^^^^^^^^^^
File "C:UsersmeAppDataLocalProgramsPythonPython311Libjson__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersmeAppDataLocalProgramsPythonPython311Libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersmeAppDataLocalProgramsPythonPython311Libjsondecoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 7 column 5 (char 12
Request help in resolving this issue.
Answers:
The error: "JSONDecodeError: Expecting property name enclosed in double quotes"
simply means json_input.json is not a proper JSON.
JSON data should be use in double quotes.
You need to fix the json file first.
If you don’t have any control over input json file. You can use Abstract Syntax Tree(ast) helper function called literal_eval like follows:
import ast
handle = open("json_input.json", "r")
content = handle.read()
data = ast.literal_eval(content)
print(type(data), data)
I am trying to convert JSON data into Python dictionary using the following code:
import json
handle = open("json_input.json", "r")
content = handle.read()
d = json.loads(content)
print(d)
But keep getting the following error:
File "C:UsersmePycharmProjectspythonProject1main.py", line 7, in <module>
d = json.loads(content)
^^^^^^^^^^^^^^^^^^^
File "C:UsersmeAppDataLocalProgramsPythonPython311Libjson__init__.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersmeAppDataLocalProgramsPythonPython311Libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersmeAppDataLocalProgramsPythonPython311Libjsondecoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
^^^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 7 column 5 (char 12
Request help in resolving this issue.
The error: "JSONDecodeError: Expecting property name enclosed in double quotes"
simply means json_input.json is not a proper JSON.
JSON data should be use in double quotes.
You need to fix the json file first.
If you don’t have any control over input json file. You can use Abstract Syntax Tree(ast) helper function called literal_eval like follows:
import ast
handle = open("json_input.json", "r")
content = handle.read()
data = ast.literal_eval(content)
print(type(data), data)