Home > database >  Error while using json in python to deserialize a string
Error while using json in python to deserialize a string

Time:10-04

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():
    print(record)
    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:\Users\veeru\Python 3.9.2\lib\tkinter\__init__.py", line 1892, in __call__
  return self.func(*args)
File "c:\Users\veeru\OneDrive\Visual Studio\Timetable\TimeTable.py", line 186, in <lambda>
  addtaskbtn.configure(command=lambda x=(btnobj, tableframe): ae.addtaskbtnclick(x[0], x[1]))
File "c:\Users\veeru\OneDrive\Visual Studio\Timetable\addevent.py", line 33, in addtaskbtnclick
  updateglobalvalues(btnobj)
File "c:\Users\veeru\OneDrive\Visual Studio\Timetable\addevent.py", line 22, in updateglobalvalues
  detailsdict[obj]=json.loads(f"{record['details']}")
File "C:\Users\veeru\Python 3.9.2\lib\json\__init__.py", line 346, in loads
  return _default_decoder.decode(s)
File "C:\Users\veeru\Python 3.9.2\lib\json\decoder.py", line 337, in decode
  obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\veeru\Python 3.9.2\lib\json\decoder.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.

CodePudding user response:

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\':[]}"'

CodePudding user response:

Your JSON string is invalid.

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

From json.org:

A value can be a string in double quotes

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:

detailsdict[obj]=json.loads(json.dumps(record['details']))

Or use ast.literal_eval:

import ast
`detailsdict[obj]=json.loads(ast.literal_eval(record['details']))`

CodePudding user response:

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():
    print(record)
    obj= record['btnobject']
    # Replace ' with "
    details = record['details'].replace("'", '"')
    detailsdict[obj]=json.loads(details)    #I am facing an error here
  • Related