troubleshooting Question

How to parse a complex python dictionary

Avatar of Steve Jennings
Steve Jennings asked on
Python
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('https://192.168.254.100/wapi/v2.11/record:host', params=params, verify=False, auth=('usr', 'pass'))
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'}]}
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')
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.
ASKER CERTIFIED SOLUTION
aikimark
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