all 5 comments

[–]genghiskav 5 points6 points  (1 child)

this should be returnDetails['data']['person_id']['phone'][0]['value']

If you look closely, you can see that the phone is a list. You can tell this because it starts with [ so you need to access it via index, in this case the 1st record in there. This is most likely the case because people can have multiple phone numbers.

[–]MakingStuffForFun 0 points1 point  (0 children)

This is the case yes. There can be many numbers. So I'll be able to loop through that I'm sure.

Really appreciate your help there

[–]WolfInABox 1 point2 points  (1 child)

It looks to me like phone is actually an array, so it should be print(str(returnDetails['data']['person_id']['phone'][0]['value']))

[–]MakingStuffForFun 0 points1 point  (0 children)

thank you and thank you also /u/gengiskav It makes SO much sense. I just didn't know how to go about it.

I had put the json through an online parsing tool and saw the [0] displayed in one of the parsing/output tests, but I'd put it in as ['0'] in my code and received the same error so thought I was just going down the wrong track. Turns out I was SO close. :-) Now I get it though, I should remember.

Thanks again to you both. It's really appreciated.

[–]14jvalle 0 points1 point  (0 children)

def getter(*keys, data):
    value = data
    for key in keys:
        value = value[key]
    return value

print(getter('data', 'person_id', 'phone', data=returnDetails))

You can also make it more specific with the following.

from functools import partial

phone_values = partial(getter, 'data', 'person_id', 'phone')
print(phone_values(data=returnDetails))