Python 解析boto3输出JSON
我正在使用此代码获取IAM用户:Python 解析boto3输出JSON,python,json,amazon-web-services,boto3,amazon-iam,Python,Json,Amazon Web Services,Boto3,Amazon Iam,我正在使用此代码获取IAM用户: #!/usr/bin/env python import boto3 import json client = boto3.client('iam') response = client.list_users( ) print response 并获得: {u'Users': [{u'UserName': 'ja', u'Path': '/', u'CreateDate': datetime
#!/usr/bin/env python
import boto3
import json
client = boto3.client('iam')
response = client.list_users(
)
print response
并获得:
{u'Users': [{u'UserName': 'ja', u'Path': '/', u'CreateDate': datetime.datetime(2018, 4, 6, 12, 41, 18, tzinfo=tzutc()), u'UserId': 'AIDAJXIHXCSI62ZQKLGZM', u'Arn': 'arn:aws:iam::233135199200:user/ja'}, {u'UserName': 'test', u'Path': '/', u'CreateDate': datetime.datetime(2018, 4, 7, 10, 55, 58, tzinfo=tzutc()), u'UserId': 'AIDAIENHQD6YWMX3A45TY', u'Arn': 'arn:aws:iam::233135199200:user/test'}], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'ebd600f0-3a52-11e8-bc50-d37153ae8ac5', 'HTTPHeaders': {'x-amzn-requestid': 'ebd600f0-3a52-11e8-bc50-d37153ae8ac5', 'date': 'Sat, 07 Apr 2018 11:00:44 GMT', 'content-length': '785', 'content-type': 'text/xml'}}, u'IsTruncated': False}
我保存了对JSON文件的响应
我只需要提取用户名,这样在这种情况下输出应该是2行:
ja
test
添加时
print response['Users'][0]['UserName']
仅获取名字,如果设置为
[]
,则语法不正确无需转换为JSON。响应作为Python对象返回:
import boto3
client = boto3.client('iam')
response = client.list_users()
print ([user['UserName'] for user in response['Users']])
这将导致:
['ja', 'test']
或者,您可以使用:
for user in response['Users']:
print (user['UserName'])
这导致:
ja
test
谢谢约翰,同时我找到了解决办法:
#/usr/bin/env python import boto3 import json client=boto3.client('iam')response=client.list_用户()范围内的i(0,len(response['users')):打印响应['users'][i]['UserName']
您还可以使用:aws iam列表用户——查询用户[*]。UserName