How to iterate through dictionary keys in python using arguments from the user * args.
Knowing the file structure, the user can select data based on the keys.
Someone has an idea how to do it, and you never know how many arguments the user will give.
class JsonOperation:
def __init__(self, filename):
self.filename = filename
self.json_out = None
def read(self):
json_file = open(self.filename, "r")
json_content = json_file.read()
self.json_out = json.loads(json_content)
print(self.json_out)
def find(self, *args):
content = {arg: self.json_out[arg] for arg in args}
for key, value in content:
result = (key, value)
print(result)
json_obj = JsonOperation("example.json")
json_obj.read()
json_obj.find('quiz', 'sport', 'q1', 'question', 'options')
This is what the json file looks like.
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket",
"Adam DDDD"
],
"answer": "Huston Rocket"
}
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": [
"10",
"11",
"12",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2",
"3",
"4"
],
"answer": "4"
}
}
}
}