This is the json file I want to parse
{
"results": [
{
"gender": "male",
"name": {
"title": "mr",
"first": "brad",
"last": "gibson"
},
"location": {
"street": "9278 new road",
"city": "kilcoole",
"state": "waterford",
"postcode": "93027",
"coordinates": {
"latitude": "20.9267",
"longitude": "-7.9310"
}
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/75.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/75.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/75.jpg"
}
}
]
}
I can easily access the first element i.e(gender) using
response = requests.get('https://randomuser.me/api')
data = response.json()
ans = data['results'][0]['gender']
print(ans)
but I am not getting how to access elements of "name" i.e title,first,last
I tried
ans = data['results'][1]['name'][0]['title']
Error: index out of bound
data['results'][1]['name'][0]. Here you are trying to use the name variable instead of a string.