简体   繁体   中英

AWS Boto3 Iterate over JSON response

I am writing a python script to get the LoadBalancerName value from the ELBv2 endpoint using Boto3.

Because the returned ?dict? has multiple levels and I do not know the index of the items, I can't specify that. My current code is below:-

import boto3
import pprint
import json

pp = pprint.PrettyPrinter(indent=4)
elbv2 = boto3.client('elbv2', region_name='eu-west-2')
response = elbv2.describe_load_balancers()
pp.pprint(response['LoadBalancers'][0]['LoadBalancerName'])

for k in response:
    print("Key: ", k)
    print( "Value: ", dict[k])

I know that the value is currently printing the key as I am not sure how to get the key as it is nested. Ideally I want to be able to print the value for the subkey LoadBalancerName

Also, I am using Python 2.7 on this as that is what is installed on the server that will run this.

There is an example of the response body for describe_load_balancers() , see THIS .

Response Syntax

{
    'LoadBalancerDescriptions': [
        {
            'LoadBalancerName': 'string',
            'DNSName': 'string',
            'CanonicalHostedZoneName': 'string',
            'CanonicalHostedZoneNameID': 'string',
            'ListenerDescriptions': [
                {
                    'Listener': {
                        'Protocol': 'string',
                        'LoadBalancerPort': 123,
                        'InstanceProtocol': 'string',
                        'InstancePort': 123,
                        'SSLCertificateId': 'string'
                    },
                    'PolicyNames': [
                        'string',
                    ]
                },
            ],
            'Policies': {
                'AppCookieStickinessPolicies': [
                    {
                        'PolicyName': 'string',
                        'CookieName': 'string'
                    },
                ],
                'LBCookieStickinessPolicies': [
                    {
                        'PolicyName': 'string',
                        'CookieExpirationPeriod': 123
                    },
                ],
                'OtherPolicies': [
                    'string',
                ]
            },
            'BackendServerDescriptions': [
                {
                    'InstancePort': 123,
                    'PolicyNames': [
                        'string',
                    ]
                },
            ],
            'AvailabilityZones': [
                'string',
            ],
            'Subnets': [
                'string',
            ],
            'VPCId': 'string',
            'Instances': [
                {
                    'InstanceId': 'string'
                },
            ],
            'HealthCheck': {
                'Target': 'string',
                'Interval': 123,
                'Timeout': 123,
                'UnhealthyThreshold': 123,
                'HealthyThreshold': 123
            },
            'SourceSecurityGroup': {
                'OwnerAlias': 'string',
                'GroupName': 'string'
            },
            'SecurityGroups': [
                'string',
            ],
            'CreatedTime': datetime(2015, 1, 1),
            'Scheme': 'string'
        },
    ],
    'NextMarker': 'string'
}

If you want to get the Load Balancer names for all, then

response = client.describe_load_balancers()
for item in response['LoadBalancerDescriptions']:
    print(item['LoadBalancerName'])

will give you the names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM