问题解析JSON文件-Python

时间:2018-04-06 09:56:39

标签: python json

将此部分放在一个大型JSON文件中

"UserDetailList": [
        {
            "UserName": "citrix-xendesktop-ec2-provisioning", 
            "GroupList": [], 
            "CreateDate": "2017-11-07T14:20:14Z", 
            "UserId": "AIDAI2YJINPRUEM3XHKXO", 
            "Path": "/", 
            "AttachedManagedPolicies": [
                {
                    "PolicyName": "AmazonEC2FullAccess", 
                    "PolicyArn": "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
                }, 
                {
                    "PolicyName": "AmazonS3FullAccess", 
                    "PolicyArn": "arn:aws:iam::aws:policy/AmazonS3FullAccess"
                }
            ], 
            "Arn": "arn:aws:iam::279052847476:user/citrix-xendesktop-ec2-provisioning"
        }, 

需要为用户

提取AttachedManagedPolicy.Policy名称

期望的输出:

"citrix-xendesktop-ec2-provisioning","AmazonEC2FullAccess"
"citrix-xendesktop-ec2-provisioning","AmazonS3FullAccess"

有些用户根本没有任何政策,因此需要一些检查机制以避免错误

with open('1.json') as file:
        data = json.load(file)
        for element in data['UserDetailList']:
            s = element['UserName'], element['AttachedManagedPolicies']
            print s

获得

(u'citrix-xendesktop-ec2-provisioning', [{u'PolicyName': u'AmazonEC2FullAccess', u'PolicyArn': u'arn:aws:iam::aws:policy/AmazonEC2FullAccess'}, {u'PolicyName': u'AmazonS3FullAccess', u'PolicyArn': u'arn:aws:iam::aws:policy/AmazonS3FullAccess'}])

添加element['AttachedManagedPolicies']['PolicyName']

得到:TypeError: list indices must be integers, not str

1 个答案:

答案 0 :(得分:1)

您收到错误是因为element['AttachedManagedPolicies']列表不是字典,您需要迭代element['AttachedManagedPolicies']然后访问密钥,如下所示:

[i['PolicyName'] for i in element['AttachedManagedPolicies']]

这将构建键PolicyName

的值列表

正如你所说,你有一个非常大的JSON结构,你可能有空值或没有值,为此你可以继续如下:

d = {
"UserDetailList": [
        {
            "UserName": "citrix-xendesktop-ec2-provisioning", 
            "GroupList": [], 
            "CreateDate": "2017-11-07T14:20:14Z", 
            "UserId": "AIDAI2YJINPRUEM3XHKXO", 
            "Path": "/", 
            "AttachedManagedPolicies": [
                {
                    "PolicyName": "AmazonEC2FullAccess", 
                    "PolicyArn": "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
                }, 
                {
                    "PolicyName": "AmazonS3FullAccess", 
                    "PolicyArn": "arn:aws:iam::aws:policy/AmazonS3FullAccess"
                }
            ], 
            "Arn": "arn:aws:iam::279052847476:user/citrix-xendesktop-ec2-provisioning"
        } 
    ]
}

user_list = d.get("UserDetailList", None)  # if unable to fetch key then it will return None
if user_list:
    for user_detail in user_list:
        username = user_detail.get("UserName", None)
        policies = [i.get('PolicyName') for i in user_detail.get('AttachedManagedPolicies', []) if i.get('PolicyName', None)]  # empty list constructed if no policy exist
        print(username, policies)