I'm quite confused on why I keep getting KeyError: None, I would like to get the user to enter a Name(e.g. Lisbon) and return other Users in the JSON file(e.g. Jade, John) that falls under the same Country as what the User Inputs(e.g. Lisbon), I have a JSON file that looks like this:
{
"user1":{
"Country":[
"China",
"USA",
"Nepal"
],
"Name":[
"Lisbon"
]
},
"user2":{
"Country":[
"Sweden",
"China",
"USA"
],
"Name":[
"Jade"
]
},
"user3":{
"Country":[
"India",
"China",
"USA"
],
"Name":[
"John"
]
}
}
And here's my code
userName = raw_input("Enter user's name: ")
with open('listOfUsers.json') as f:
data = json.load(f)
def getId(name):
for userId, v in data.items():
if v['Name'] == name:
return userId;
id = getId(userName)
for k, v in data.items():
if any(x in data[id]['Country'] for x in v['Country']):
print(k)
How do I go around this problem?