Why can I not deserialise a JSON string with single quotes in Python?

I am using json to deserialize a string that is received from my MySQL database such that the resulting object is a dictionary.
This is the code:

sql.cursorobj.execute("SELECT * FROM timetableevents")
for record in sql.cursorobj.fetchall():
    obj= record['btnobject']
    detailsdict[obj]=json.loads(record['details'])    #I am facing an error here

The error I am getting is:

{'btnobject': 'btnobject1', 'details': "{'sno':[], 'time':[], 'events':[]}"}
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersveeruPython 3.9.2libtkinter__init__.py", line 1892, in __call__
  return self.func(*args)
File "c:UsersveeruOneDriveVisual StudioTimetableTimeTable.py", line 186, in <lambda>
  addtaskbtn.configure(command=lambda x=(btnobj, tableframe): ae.addtaskbtnclick(x[0], x[1]))
File "c:UsersveeruOneDriveVisual StudioTimetableaddevent.py", line 33, in addtaskbtnclick
File "c:UsersveeruOneDriveVisual StudioTimetableaddevent.py", line 22, in updateglobalvalues
File "C:UsersveeruPython 3.9.2libjson__init__.py", line 346, in loads
  return _default_decoder.decode(s)
File "C:UsersveeruPython 3.9.2libjsondecoder.py", line 337, in decode
  obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersveeruPython 3.9.2libjsondecoder.py", line 353, in raw_decode
  obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

The last line in the above error code is of most importance ig.
I have tried enclosing the record['details'] in double quotes explicitly
I have also used fstrings like: json.loads(f"{record['details']}")
**NOTE: **
Keep in mind that json.loads(record['details']) worked previously, but after I changed my database, it stopped working.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

if you try to load

print(json.loads('{"sno":[], "time":[], "events":[]}'))

it will give you no error cause key is in double qoute,
and you’ll get the result

{'sno': [], 'time': [], 'events': []}

but if you cant change the output then try json.dumps first

print(json.dumps("{'sno':[], 'time':[], 'events':[]}"))

this will give you

'"{'sno':[], 'time':[], 'events':[]}"'

Method 2

Your JSON string is invalid.

From json.org:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.

It has single quotes while JSON strings should have double quotes to be valid.

You don’t really have the option to mass replace the quotation marks from the database so use json.dumps before loading the JSON to convert the quotation marks.

Try this:


Or use ast.literal_eval:

import ast

Method 3

You have single quotes in our string and hence you get this error. What you can do is replace ' with " and then do a json.loads as below:

sql.cursorobj.execute("SELECT * FROM timetableevents")
for record in sql.cursorobj.fetchall():
    obj= record['btnobject']
    # Replace ' with "
    details = record['details'].replace("'", '"')
    detailsdict[obj]=json.loads(details)    #I am facing an error here

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x