Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Searching a multi-dimensional dictionary for a key

I'm using Python's JSON decoding library with Google Maps API. I am trying to obtain the zip code of an address but it sometimes resides in different dictionary key. Here are two examples (I've trimmed the JSON to what is relevant):

placemark1 = {
  "AddressDetails": {
    "Country": {
      "AdministrativeArea": {
        "SubAdministrativeArea": {
          "Locality": {
            "PostalCode": {
              "PostalCodeNumber": "94043"
            }
          }
        }
      }
    }
  }
}

(View full JSON)

placemark2 = {
  "AddressDetails": {
    "Country" : {
      "AdministrativeArea" : {
        "Locality" : {
          "PostalCode" : {
            "PostalCodeNumber" : "11201"
          }
        }
      }
    }
  }
}

(View full JSON)

So the zipcodes:

zipcode1 = placemark1['AddressDetails']['Country']['AdministrativeArea']['SubAdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']
zipcode2 = placemark2['AddressDetails']['Country']['AdministrativeArea']['Locality']['PostalCode']['PostalCodeNumber']

Now I was thinking perhaps I should just search the multi-dimensional dictionary for "PostalCodeNumber" key. Does anyone have any idea on how to accomplish 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
like image 648
Belmin Fernandez Avatar asked Sep 14 '26 01:09

Belmin Fernandez


1 Answers

def get_key(key,dct):
    if key in dct:
        return dct[key]
    for k in dct:
        try:
            return get_key(key,dct[k])
        except (TypeError,ValueError):
            pass
    else:
        raise ValueError

placemark1 = {
  "AddressDetails": {
    "Country": {
      "AdministrativeArea": {
        "SubAdministrativeArea": {
          "Locality": {
            "PostalCode": {
              "PostalCodeNumber": "94043"
            }
          }
        }
      }
    }
  }
}

placemark2 = {
  "AddressDetails": {
    "Country" : {
      "AdministrativeArea" : {
        "Locality" : {
          "PostalCode" : {
            "PostalCodeNumber" : "11201"
          }
        }
      }
    }
  }
}

just_being_a_dict = {}
just_a_list = []
counter_dict = {'Name': 'I like messing things up'}

for x in (placemark1, placemark2, just_being_a_dict, just_a_list, counter_dict):
    try:
        print(get_key('PostalCodeNumber', x))
    except ValueError:
        print(None)

yields

94043
11201
None
None
None
like image 154
unutbu Avatar answered Sep 16 '26 16:09

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!