Home > Enterprise >  python tkinter not updating json file
python tkinter not updating json file

Time:03-15

Im learning how to update, write and read json files in python.

When I update my json file with exception handling it gives an error:

Exception in Tkinter callback Traceback (most recent call last):  
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/__init__.py",
line 1921, in __call__
    return self.func(*args)   File "/Users/montekkundan/Downloads/coding/python/password-manager/main.py",
line 53, in save
    data = json.load(data_file)   File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py",
line 293, in load
    return loads(fp.read(),   File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py",
line 346, in loads
    return _default_decoder.decode(s)   File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py",
line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())   File "/Library/Frameworks/Python.framework/Versions/3.10/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)

Process finished with exit code 0

python function:

def save():
    website = website_entry.get()
    email = email_entry.get()
    password = password_entry.get()
    new_data = {
        website: {
            "email": email,
            "password": password,
        }
    }

    if len(website) == 0 or len(password) == 0:
        messagebox.showerror(title="Oops!", message="Please make sure you haven't left any fields empty.")

    else:
        
        try:
            with open("data.json", "r") as data_file:
                # Reading old data
                data = json.load(data_file)
        except FileNotFoundError:
            with open("data.json", "w") as data_file:
                json.dump(new_data, data_file, indent=4)
        else:
            # Updating old data with new data
            data.update(new_data)

            with open("data.json", "w") as data_file:
                # Saving updated data
                json.dump(data, data_file, indent=4)
        finally:
            website_entry.delete(0, END)
            password_entry.delete(0, END)

CodePudding user response:

Check what you have in file - it seems it is empty.
And empty file/string is uncorrect JSON and it raises error.

You should rather create new empty dict data when it can't find file or it has problem to read it.

try:
    with open("data.json", "r") as data_file:
        # Reading old data
        data = json.load(data_file)
except FileNotFoundError:
    print("Problem: FileNotFoundError")
    data = dict()
except json.JSONDecodeError:
    print("Problem: JSONDecodeError")
    data = dict()

finally:
    
    # --- always ---
    
    data.update(new_data)
    
    with open("data.json", "w") as data_file:
        # Saving updated data
        json.dump(data, data_file, indent=4)
    
    website_entry.delete(0, END)
    password_entry.delete(0, END)
  • Related