0

I am trying to save my objects created, stored in the __objects class attribute, and I am getting error:

Traceback (most recent call last):
  File "/home/leuel/PycharmProjects/AirBnB_clone/./test_save_reload_base_model.py", line 2, in <module>
    from models import storage
  File "/home/leuel/PycharmProjects/AirBnB_clone/models/__init__.py", line 4, in <module>
    storage.reload()
  File "/home/leuel/PycharmProjects/AirBnB_clone/models/engine/file_storage.py", line 32, in reload
    self.__objects = json.load(f)
  File "/usr/lib/python3.10/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/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)

The method that I used to save to the json file:

def save(self):
    with open(self.__file_path, "w+") as f:
        json.dump(self.__objects, f)

The __objects attribute is a class attribute that is a dictionary holding all instances (instances will be stored using a key based on their ids). Why am I getting the error? Initially, the __objects will be an empty dictionary

And another question when referencing class attributes, should I use self or should I write the class name? If I should write the class name, is it because instances might change the class attribute? I am new to python and JSON, so excuse me if my question is not clear enough.

EDIT: As @Richard Neumann, pointed out, my error comes from json.load(). Here is the code where I called the json.load():

    def reload(self):
    if path.isfile(self.__file_path):
        with open(self.__file_path, "r", encoding="utf-8") as f:
            self.__objects = json.load(f)

The reload method will reload the __objects class attribute from a file.json file. I did this to recreate instances when I reopen my program

12
  • 1
    Why are you using json.dump twice in your save() method? Did you mean: json.dump(self.__objects, f)? Commented Oct 28, 2022 at 9:00
  • To reference class attributes you have to use self inside the class methods. If they are public and you are calling them outside the class,then use the name of the instance. Commented Oct 28, 2022 at 9:03
  • @quamrana I was trying to come up with a solution, because json.dump(self.__objects, f) still gives me the same error Commented Oct 28, 2022 at 9:04
  • 1
    The error occurs on json.load(), not on json.dump(). Show us the actual code where the error occurs, i.e. where you invoke json.load() as well as the contents of the respective file. Commented Oct 28, 2022 at 9:09
  • 1
    Well no. It should be an empty dict. ie {} Commented Oct 28, 2022 at 9:17

1 Answer 1

1

you already encoded your dictionary to a valid JSON data when you use json.dumps but when you try to dump it again with json.dump, python gives an error because json.dumps expects a valid python dictionary, not a valid JSON Data, and since you're trying to write to a file, use

f.write(json_string)

instead of json.dump

Sign up to request clarification or add additional context in comments.

1 Comment

This is plain wrong. You can safely json.dump{,s} a string: json.dumps(json.dumps({}))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.