Home > Software engineering >  How to read multiple JSON entry's without python error
How to read multiple JSON entry's without python error

Time:02-20

I am trying to make a simple python program with a tkinter gui that can take a user input and add it to a JSON file. Then there will be another another input where the user can put a name and find the data entered with that name. So far i am able to write the information to the JSON file. The key is automatically the name followed by the data type. That way when a user inputs the name my program can add the datatype to the end of the name and have the key for required data. This system works only when there is one object in the JSON file. When I add 2 i get the following error

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\liamm\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "c:\Users\liam\Desktop\Coding\json test\datagui.py", line 17, in find
    Age = entry[Agevar]
KeyError: 'Steve_Age'

I am not sure why i am getting a tkinter error and a key error when the key it says has an error is a vaild key in the JSON file.

Here is the JSON file with some test data in it however more will be created.

[
    {
        "Bob_NAME": "Bob",
        "Bob_Age": "40",
        "Bob_Weight": "200"
    },
    {
        "Steve_NAME": "Steve",
        "Steve_Age": "30",
        "Steve_Weight": "150"
    }
]

Here is my python program

from tkinter import *
from tkinter import ttk
import json
from tokenize import Name
from setuptools import Command

 

filename = "test.json"

def find():
        with open (filename, "r") as f:
            finder = json.load(f)
#Adding the name and _Age to find the key for the right information
        Agevar = E4.get() "_Age"
        for entry in finder:
                Age = entry[Agevar]
                print(f"Age: {Age}")






def add_data():





    Namevar = E1.get() "_NAME" 
    Agevar = E1.get() "_Age"
    Weightvar = E1.get() "_Weight"

    item_data = {}
    with open (filename, "r") as f:
            temp = json.load(f)
    item_data[Namevar] = E1.get()
    item_data[Agevar] = E2.get()
    item_data[Weightvar] = E3.get()
    temp.append(item_data)
    with open (filename, "w") as f:
            json.dump(temp, f, indent=4)
            print(Namevar)
            print(Agevar)
            print(Weightvar)
            






top = Tk()

top.geometry("500x300")

L1 = Label(top, text="Enter information")
L1.grid(row=1, column=1)

L1 = Label(top, text="Name:")
L1.grid(row=2, column=1)

L1 = Label(top, text="Age:")
L1.grid(row=3, column=1)

L1 = Label(top, text="Weight:")
L1.grid(row=4, column=1)

L1 = Label(top, text="Find: ")
L1.grid(row=3, column=3)



E1 = Entry(top, bd =5)
E1.grid(row=2, column= 2)

E2 = Entry(top, bd =5)
E2.grid(row=3, column= 2)

E3 = Entry(top, bd =5)
E3.grid(row=4, column= 2)


E4 = Entry(top, bd= 5)
E4.grid(row= 3, column= 4)

btn = Button(top, command = add_data, text = "Add", height = 2, width = 5)
btn.grid(row=5, column=2)

btn2 = Button(top, command = find, text = "Find", height = 2, width = 5)
btn2.grid(row=5, column=4)

top.mainloop()

CodePudding user response:

The issue stems from the following line:

for entry in finder:

The reason for that is because finder is a list of dictionaries. The first item in that list references Bob. It appears the code you are running is looking for Steve, which is the second item in that list. You'll need to place some logic regarding Agevar = E4.get() "_Age" since this can return any individuals name.

You can change the code to the following:

for entry in finder:
    Age = entry.get(Agevar)
    if Age:
        print(f"Age: {Age}")

This however, won't handle a case where a name is entered that does not exist at all.

  • Related