troubleshooting Question

How to parse a complex python dictionary

Avatar of Steve Jennings
Steve Jennings asked on
2 Comments1 Solution16 ViewsLast Modified:
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'))
And I get the following response:
{'result': [
   {'_ref': 'record:host/', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:', 'host': '', 'ipv4addr': ''}], 'name': '', 'view': 'default'},
   {'_ref': 'record:host/', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:', 'host': '', 'ipv4addr': ''}], 'name': '', 'view': 'default'},
   {'_ref': 'record:host/', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:',  'host': '', 'ipv4addr': ''}], 'name': '', 'view': 'default'},
   {'_ref': 'record:host/', 'ipv4addrs': [
   {'_ref': 'record:host_ipv4addr/ZG5zLmhvc:', 'host': '', 'ipv4addr': ''}], 'name': '', 'view': 'default'}]}
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')
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.
Get vaccinated; Social distance; Wear a mask

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2014

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros