pickledb icon indicating copy to clipboard operation
pickledb copied to clipboard

Getting JSONDecodeError

Open rmnijgh opened this issue 3 years ago • 4 comments

Hello,

I'm getting JSON errors, and I'm not sure if I'm doing something wrong, or if its a bug.

I'm getting this error on 3 different machines (2 Ubuntu, 1 Windows). One Ubuntu and Windows use python3.9, the other Ubuntu python3.8. All PickleDB's modules have been installed today via pip.

Code:

#/usr/bin/python3
import pickledb

db = pickledb.load('pik.db', False)
db.set('foo', 'bar')

print(db.get('foo'))

When I run it:

ubuntu@mimas:~/pik$ python3 pik.py 
Traceback (most recent call last):
  File "pik.py", line 5, in <module>
    db = pickledb.load('pik.db', False)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pickledb.py", line 43, in load
    return PickleDB(location, auto_dump, sig)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pickledb.py", line 54, in __init__
    self.load(location, auto_dump)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pickledb.py", line 85, in load
    self._loaddb()
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pickledb.py", line 102, in _loaddb
    self.db = json.load(open(self.loco, 'rt'))
  File "/usr/lib/python3.8/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Like to hear from you guys.

rmnijgh avatar Mar 11 '22 13:03 rmnijgh

hey, can you send the .db file looks like there is some invalid value in it

tory1103 avatar Apr 30 '22 19:04 tory1103

I don't have it anymore but I know how I got it fixed. I only get the error when I made the file myself, If I don't make the file and let pickledb create it I don't get the error. Something wrong with creating the db file on my own instead of letting the module doing it for me?

rmnijgh avatar Apr 30 '22 20:04 rmnijgh

It shouldn't be any problem when writting database manually. This project its unmantained, if you keep having errors try using my enhanced version of pickledb https://github.com/tory1103/oh-my-pickledb. It solves many pickledb errors....

PD: if the problem is solved, make sure the issue is closed!

tory1103 avatar Apr 30 '22 20:04 tory1103

When writing the database manually, you need to pay attention to the difference between ' and '' in Python/JSON.

>>> import json
>>> json.dumps({'key': 'value'})
'{"key": "value"}'

markoxu avatar May 15 '22 09:05 markoxu