Printing nested json – Python3

Question:

I have the following json output assigned to a variable called "mydict":

{
  "data": {
    "endpoints": {
      "edges": [
        {
          "node": {
            "name": "test1.net",
            "ipAddress": "1.2.3.4"
          }
        },
        {
          "node": {
            "name": "test2.net",
            "ipAddress": "4.3.2.1"
          }
        },
        {
          "node": {
            "name": "test3.net",
            "ipAddress": "0.0.0.0"
          }
        }
      ]
    }
  }
}

I’m able to print out a single "name" value with:

print("Dictionary contains: ",mydict['data']['endpoints']['edges'][1]['node']['name'])

But with how the data is nested, I’m not sure how to iterate through each "name" to get a print of only those values. Any recommendations? Thank you!

Asked By: axxic3

||

Answers:

for edge in mydict['data']['endpoints']['edges']:
    print(edge['node']['name'])
Answered By: John Gordon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.