Home > Mobile >  Printing nested json - Python3
Printing nested json - Python3

Time:11-01

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!

CodePudding user response:

for edge in mydict['data']['endpoints']['edges']:
    print(edge['node']['name'])
  • Related