Avatar of Steve Jennings
Steve Jennings
 asked on

How to parse a complex python dictionary

I am using python 3.8.5 to get data from an Infoblox appliance to determine the number of new entries for a given client name. In the example I've posted the client is ykk. I send the following search string:

params = (
    ('_return_as_object', '1'),
    ('name~', 'ykk'),

response = requests.get('', params=params, verify=False, auth=('usr', 'pass'))

Open in new window

And I get the following response:
{'result': [
   {'_ref': 'record:host/ZG5zLmhvc:ykk1.tunnel.com/default', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:', 'host': 'ykk1.tunnel.com', 'ipv4addr': ''}], 'name': 'ykk1.tunnel.com', 'view': 'default'},
   {'_ref': 'record:host/ZG5zLmhvc:ykk-sec.tunnel.com/default', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:', 'host': 'ykk-sec.tunnel.com', 'ipv4addr': ''}], 'name': 'ykk-sec.tunnel.com', 'view': 'default'},
   {'_ref': 'record:host/ZG5zLmhvc:ykk2.tunnel.com/default', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:',  'host': 'ykk2.tunnel.com', 'ipv4addr': ''}], 'name': 'ykk2.tunnel.com', 'view': 'default'},
   {'_ref': 'record:host/ZG5zLmhvc:ykk2-sec.tunnel.com/default', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:', 'host': 'ykk2-sec.tunnel.com', 'ipv4addr': ''}], 'name': 'ykk2-sec.tunnel.com', 'view': 'default'}]}

Open in new window

All I need from the return is the hostname and the IP address. I may get a single DNS record in return or I may get multiple records. So I try and loop through the dictionary using this code:
resarr = []

len1 = (out.keys())
len2 = len(out['result'][0]['ipv4addrs'][0]['host'])

   for i in range(0, len2):
      drec = (out['result'][i]['ipv4addrs'][0]['host'], out['result'][i]['ipv4addrs'][0]['ipv4addr'] )
except IndexError:
   print('uh oh')

Open in new window

My len1 and len2 don't provide useful values, certainly not the correct return value for what I need, and len2 is always larger than actual number of DNS entries returned so I can simply let it throw an IndexError and get all the data I need.

So I have 2 issues that are eluding me. First, I have tried several different variations on the len() function to see how many records are returned. I suspect this doesn't work because I am getting a nested dictionary back. Second, it seems like poor code to just let it throw an IndexError to stop the for loop.

I'm not sure what to try next. Any guidance would be appreciated.

Avatar of undefined
Last Comment
Steve Jennings

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Steve Jennings

Thanks akiamark! I don't care so much about elegance, right now I am fully into results.

Thanks again!

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy