I'm trying to write a Python script to read data from a JSON file, do some calculations with it and then write output to a new JSON file. But I can't seem to automate the JSON reading process. I get this error. Could you please help me with this issue? Thank you very much
print([a[0]][b[1]][c[1]])
TypeError: list indices must be integers or slices, not str
test.json
{
"male": {
"jack": {
"id": "001",
"telephone": "+31 2225 345",
"address": "10 Street, Aukland",
"balance": "1500"
},
"john": {
"id": "002",
"telephone": "+31 6542 365",
"address": "Main street, Hanota",
"balance": "2500"
}
},
"female": {
"kay": {
"id": "00",
"telephone": "+31 6542 365",
"address": "Main street, Kiro",
"balance": "500"
}
}
}
test.py
with open("q.json") as datafile:
data = json.load(datafile)
a = ['male', 'female']
b = ['jack', 'john', 'kay']
c = ['id', 'telephone', 'address', 'balance']
print([a[1]][b[1]][c[1]])
male -> John -> telephoneprint(data[a[0]][b[1]][c[1]])? --> Note:keysare case sensitivedata = json.load(datafile),datawill bedicttype. So you can accessmale -> John -> telephonewithdata['Male']['John']['telephone'].