I am using the Python JSON decoding library with the Google Maps API. I am trying to get the zip code of an address, but sometimes it is in a different dictionary. Here are two examples (I clipped JSON to what is appropriate):
placemark1 = {
"AddressDetails": {
"Country": {
"AdministrativeArea": {
"SubAdministrativeArea": {
"Locality": {
"PostalCode": {
"PostalCodeNumber": "94043"
}
}
}
}
}
}
}
( View full JSON )
placemark2 = {
"AddressDetails": {
"Country" : {
"AdministrativeArea" : {
"Locality" : {
"PostalCode" : {
"PostalCodeNumber" : "11201"
}
}
}
}
}
}
( View full JSON )
So zipcodes:
zipcode1 = placemark1['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']
zipcode2 = placemark2['AddressDetails']['Country']['AdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']
Now I thought that I should just look for a multidimensional dictionary for a key "PostalCodeNumber". Does anyone know how to do this? I want it to look something like this:
>>> just_being_a_dict = {}
>>> just_a_list = []
>>> counter_dict = {'Name': 'I like messing things up'}
>>> get_key('PostalCodeNumber', placemark1)
"94043"
>>> get_key('PostalCodeNumber', placemark2)
"11201"
>>> for x in (just_being_a_dict, just_a_list, counter_dict):
... get_key('PostalCodeNumber', x) is None
True
True
True
source
share