Search code examples
pythonboto3

Extract List of Dictionary in Python


I have the following list:

    {
   "TargetHealthDescriptions":[
      {
         "Target":{
            "Id":"10.101.100.101",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      },
      {
         "Target":{
            "Id":"10.102.100.102",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      },
      {
         "Target":{
            "Id":"10.103.100.103",
            "Port":8200,
            "AvailabilityZone":"all"
         },
         "HealthCheckPort":"8200",
         "TargetHealth":{
            "State":"healthy"
         }
      }
   ],
   "ResponseMetadata":{
      "RequestId":"abcdef-01234-4d84-9aaf-0123456789",
      "HTTPStatusCode":200,
      "HTTPHeaders":{
         "x-amzn-requestid":"870c5177-482b-4d84-9aaf-0123456789",
         "content-type":"text/xml",
         "content-length":"1279",
         "date":"Mon, 11 Jul 2022 15:35:38 GMT"
      },
      "RetryAttempts":0
   }
}

How can I extract each target Id? I basically need to extract update these IPs if a dig command does not match. So far the only thing I have been able to do is print ['TargetHealthDescriptions'][0] but whenever I try iterate over the list I get a "<generator object at 0x10522c890>" so not sure what that even means.


Solution

  • You need to identify and iterate over the list from within the dictionary by using it's key. The key in this dictionary is the_response['TargetHealthDescriptions'].

    Once you have that you can then iterate over it like any other list treating each result as a dictionary, getting the value within it you require, which in this case is ['Target']['Id'] so it looks like this:

    for target in the_response['TargetHealthDescriptions']:
        print(target['Target']['Id'])
    

    Minimal example below:

    the_response =     {
       "TargetHealthDescriptions":[
          {
             "Target":{
                "Id":"10.101.100.101",
                "Port":8200,
                "AvailabilityZone":"all"
             },
             "HealthCheckPort":"8200",
             "TargetHealth":{
                "State":"healthy"
             }
          },
          {
             "Target":{
                "Id":"10.102.100.102",
                "Port":8200,
                "AvailabilityZone":"all"
             },
             "HealthCheckPort":"8200",
             "TargetHealth":{
                "State":"healthy"
             }
          },
          {
             "Target":{
                "Id":"10.103.100.103",
                "Port":8200,
                "AvailabilityZone":"all"
             },
             "HealthCheckPort":"8200",
             "TargetHealth":{
                "State":"healthy"
             }
          }
       ],
       "ResponseMetadata":{
          "RequestId":"abcdef-01234-4d84-9aaf-0123456789",
          "HTTPStatusCode":200,
          "HTTPHeaders":{
             "x-amzn-requestid":"870c5177-482b-4d84-9aaf-0123456789",
             "content-type":"text/xml",
             "content-length":"1279",
             "date":"Mon, 11 Jul 2022 15:35:38 GMT"
          },
          "RetryAttempts":0
       }
    }
    
    for target in the_response['TargetHealthDescriptions']:
        print(target['Target']['Id'])