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?

3
  • have you tried anything ? Commented Oct 25, 2019 at 21:04
  • As a new user, start with the tour and read How to Ask. Concerning your task, decode the JSON and extract the relevant info from the data. Commented Oct 25, 2019 at 21:04
  • 1
    Here just a quick google search away from your answer :) Commented Oct 25, 2019 at 21:07

1 Answer 1

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']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.