簡體   English   中英

如何使用 boto3 返回嵌套的 json 值

[英]How to return a nested json value with boto3

使用 python3、boto3 和 AWS。 試圖指定返回一個值而不是所有正常返回的值。

我有:

response = ec2client.describe_network_acls()
print(response["Associations"])

我得到了所有三個關聯:

[{'NetworkAclId': 'acl-58***221', 'NetworkAclAssociationId': 'aclassoc-267***56', 'SubnetId': 'subnet-e5###6bf'}, {'NetworkAclId': 'acl-5823###1', 'NetworkAclAssociationId': 'aclassoc-0a2###7a', 'SubnetId': 'subnet-ec0c###4'}]

但是,我只想獲取subnet id

提前致謝,感謝任何幫助!

response = ec2client.describe_network_acls()
subnet_ids = list(map(lambda assoc: assoc["SubnetId"], response))
print(subnet_ids)

如果你需要代碼

import boto3

ec2_client=boto3.client('ec2')

response = ec2_client.describe_network_acls()

for acl in response['NetworkAcls']:
    for association in acl['Associations']:
        print(association['SubnetId'])

訣竅是查看 boto3 文檔中的示例輸出以確定可用的字段和列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM