使用jq解析JSON文件,将其转换为CSV - 将2列合并为一列

时间:2018-03-14 08:57:22

标签: json bash jq

JSON文件:https://1drv.ms/u/s!AizscpxS0QM4hJl_VRQaWbm6D8T8_w

文件的一部分:

"RoleDetailList": [
        {
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17", 
                "Statement": [
                    {
                        "Action": "sts:AssumeRoleWithSAML", 
                        "Effect": "Allow", 
                        "Condition": {
                            "StringEquals": {
                                "SAML:aud": "https://signin.aws.amazon.com/saml"
                            }
                        }, 
                        "Principal": {
                            "Federated": "arn:aws:iam::279052847476:saml-provider/CompanyADFS"
                        }
                    }
                ]
            }, 
            "RoleId": "AROAJANL3L2IZ6UISEGAU", 
            "CreateDate": "2016-08-20T13:51:07Z", 
            "InstanceProfileList": [], 
            "RoleName": "ADFS-Administrators", 
            "Path": "/", 
            "AttachedManagedPolicies": [], 
            "RolePolicyList": [
                {
                    "PolicyName": "Administrator-Access", 
                    "PolicyDocument": {
                        "Version": "2012-10-17", 
                        "Statement": [
                            {
                                "Action": "*", 
                                "Resource": "*", 
                                "Effect": "Allow", 
                                "Sid": "Stmt1434192117145"
                            }
                        ]
                    }
                }
            ], 
            "Arn": "arn:aws:iam::279052847476:role/ADFS-Administrators"
        },

在其他部分"AttachedManagedPolicies": [],不为空,它具有以下结构:

"AttachedManagedPolicies": [
                {
                    "PolicyName": "pol-amtest-ro", 
                    "PolicyArn": "arn:aws:iam::279052847476:policy/pol-amtest-ro"
                }
            ]

我得到了所有需要的专栏,只需将第二名和第三名合并为一名:

jq -rc '.RoleDetailList
| map(select((.AssumeRolePolicyDocument.Statement | length > 0) and 
      (.AssumeRolePolicyDocument.Statement[].Principal.Federated) or 
      (.AttachedManagedPolicies | length >0) or
      (.RolePolicyList | length > 0)) )[]  
| [.RoleName,
   .RolePolicyList[].PolicyName,
   ([.AttachedManagedPolicies[].PolicyName] | join("::")),
   (.AssumeRolePolicyDocument.Statement[] | .Principal.Federated)]
| @csv' output.json

输出:

"ADFS-Administrators","Administrator-Access","","arn:aws:iam::279052847476:saml-provider/CompanyDFS"

如何摆脱"","(仅在第一行获取)?

尝试(.RolePolicyList[].PolicyName + " " + .AttachedManagedPolicies[].PolicyName)

然后获得"ADFS-Administrators","arn:aws:iam::279052847476:saml-provider/CompanyADFS"

完全失去2和3列的值

1 个答案:

答案 0 :(得分:0)

需要将其包装到数组中并应用连接

jq -rc '.RoleDetailList
| map(select((.AssumeRolePolicyDocument.Statement | length > 0) and 
    (.AssumeRolePolicyDocument.Statement[].Principal.Federated) or 
    (.AttachedManagedPolicies | length >0) or 
    (.RolePolicyList | length > 0)) )[]
| [.RoleName,
   ([.RolePolicyList[].PolicyName,
     ([.AttachedManagedPolicies[].PolicyName] | join("--"))] 
    | join(" ")),
   (.AssumeRolePolicyDocument.Statement[] | .Principal.Federated)] 
| @csv' output.json