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('https://192.168.254.100/wapi/v2.11/record:host', 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:169.254.101.0/ykk1.tunnel.com/default', 'host': 'ykk1.tunnel.com', 'ipv4addr': '169.254.101.0'}], 'name': 'ykk1.tunnel.com', 'view': 'default'},
   {'_ref': 'record:host/ZG5zLmhvc:ykk-sec.tunnel.com/default', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:169.254.201.0/ykk-sec.tunnel.com/default', 'host': 'ykk-sec.tunnel.com', 'ipv4addr': '169.254.201.0'}], 'name': 'ykk-sec.tunnel.com', 'view': 'default'},
   {'_ref': 'record:host/ZG5zLmhvc:ykk2.tunnel.com/default', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:169.254.101.1/ykk2.tunnel.com/default',  'host': 'ykk2.tunnel.com', 'ipv4addr': '169.254.101.1'}], 'name': 'ykk2.tunnel.com', 'view': 'default'},
   {'_ref': 'record:host/ZG5zLmhvc:ykk2-sec.tunnel.com/default', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:169.254.201.1/ykk2-sec.tunnel.com/default', 'host': 'ykk2-sec.tunnel.com', 'ipv4addr': '169.254.201.1'}], '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'])

try:
   for i in range(0, len2):
      drec = (out['result'][i]['ipv4addrs'][0]['host'], out['result'][i]['ipv4addrs'][0]['ipv4addr'] )
      resarr.append(drec)
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.
Python

Avatar of undefined
Last Comment
Steve Jennings

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
aikimark

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Steve Jennings

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

Thanks again!

Steve
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