Using Python to parse JSON & API’s

[
    {
        "_ref": "record:host/ZG5zLmhvc3QkLjIuY29tLnJheW92YWMubmFvcHBzd2luMDE:naoppswin01.packetpros.com/Internal",
        "ipv4addrs": [
            {
                "_ref": "record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuMi5jb20ucmF5b3ZhYy5uYW9wcHN3aW4wMS4xMC44MC4wLjIyNy4:10.80.0.227/server01.packetpros.com/Internal",
                "configure_for_dhcp": false,
                "host": "server01.packetpros.com",
                "ipv4addr": "10.80.0.227"
            }
        ],
        "name": "naoppswin01.packetpros.com",
        "view": "Internal"
    }
]

The data above is the result of querying an IP record in Infoblox. This is one of the worst implementations of JSON I’ve seen. Why is it so bad? If you know your python you will see this is a list and inside that list is a dictionary and in that dictionary is another list and then another dictionary inside of that. So that’s a [ { [ { } ] } ]. I try and write it out like that when it gets over complicated like that. So let’s assign a variable to the value of “host”. What would that look like?

The first list would get stripped (assuming we are looping over this data). So our first section is a dictionary. We have four key’s we can access: “_ref”, “ipv4addrs”, “name” and “view”. Since we are trying to get the value from the key “host”, we will want to go into the “ipv4addrs”. Since this is a dictionary, we access it with

['ipv4addrs']

Inside “ipv4addrs” is a list, look for the [. There is only one list here, but we still specify it.

[0]

Now that we are in our list we can see we have a dictionary with following keys: “_ref”, “configure_for_dhcp”, “host” and “ipv4addr”. We want to get the name so we’ll grab the key of “host”.

['host']

Now we put it all together and get:

['ipv4addrs'][0]['host']

If we didn’t want to loop over the data and just grab a single “host” we would have to add the original list first.

[0]['ipv4addrs'][0]['host']

Pretty ugly right? By figuring out the order of lists and dictionaries though it does become manageable.

For reference, here’s a functional code block.

response = requests.get('https://infoblox_url/wapi/v2.9/record:host?ipv4add=10.80.0.227', headers=headers, verify=False)
raw_data = response.json()

for node in raw_data['result']:
    host = node['ipv4addrs'][0]['host']
    ip_addy = node['ipv4addrs'][0]['ipv4addr']

print(host, ip_addy)