0

I am pulling out a collection of devices that are downed on our network.

I am running:

rjson = r.json()
print(json.dumps(rjson, indent = 5))

And it returns:

{
     "paging": {
          "size": 13
     },
     "data": {
          "devices": [
               {
                    "hostName": "host_1",
                    "networkAddress": "111.111.111.111",
                    "bestState": "Down",
                    "worstState": "Down",
               },
               {
                    "hostName": "host_2",
                    "networkAddress": "111.111.111.111",
                    "bestState": "Down",
                    "worstState": "Down",
               },
               {
                    "hostName": "host_3",
                    "networkAddress": "111.111.111.111",
                    "bestState": "Down",
                    "worstState": "Down",
               },

I would like to run though this json and create a list with each hostName that is returned. How would I do this?

David
  • 11

1 Answers1

1

you can try:

from operator import itemgetter

host_names = list(map(itemgetter('hostName'), rjson['data']['devices']))

print(host_names)

output:

['host_1', 'host_2', 'host_3']
kederrac
  • 16,819
  • 6
  • 32
  • 55