4

JSON file:

"UserDetailList": [
{
    "UserName": "citrix-xendesktop-ec2-provisioning",
    "GroupList": [],
    "CreateDate": "2017-11-07T14:20:14Z",
    "UserId": "1234556",
    "Path": "/",
    "AttachedManagedPolicies": [
        {
            "PolicyName": "AmazonEC2FullAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
        },
        {
            "PolicyName": "AmazonS3FullAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/AmazonS3FullAccess"
        }
    ],
    "Arn": "arn:aws:iam::1234567890:user/citrix-xendesktop-ec2-provisioning"
},
{
    "UserName": "rundeck-read-only-iam-permissions",
    "GroupList": [],
    "CreateDate": "2018-03-09T11:13:38Z",
    "UserId": "AIDAJQOQGKISLCWDXG6EQ",
    "Path": "/",
    "AttachedManagedPolicies": [
        {
            "PolicyName": "IAMReadOnlyAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/IAMReadOnlyAccess"
        }
    ],
    "Arn": "arn:aws:iam::279052847476:user/rundeck-read-only-iam-permissions"
}]

with jq -r '.UserDetailList[] | [.UserName] | @csv' output.json > fileout2.csv

I can get

citrix-xendesktop-ec2-provisioning"
"rundeck-read-only-iam-permissions"

How to get IAM policies for these 2 users, i need to extract AmazonEC2FullAccess and AmazonS3FullAccess under AttachedManagedPolicies ?

so output can be

citrix-xendesktop-ec2-provisioning",AmazonEC2FullAccess
citrix-xendesktop-ec2-provisioning",AmazonS3FullAccess
rundeck-read-only-iam-permissions,IAMReadOnlyAccess

closed as off-topic by Jeff Schaller, Timothy Martin, DopeGhoti, don_crissti, G-Man Mar 10 '18 at 5:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Jeff Schaller, Timothy Martin, DopeGhoti, don_crissti, G-Man

10

jq solution:

jq -r '.UserDetailList[] | .UserName as $u 
       | .AttachedManagedPolicies[] | ([$u, .PolicyName] | @csv)' input.json

The output:

"citrix-xendesktop-ec2-provisioning","AmazonEC2FullAccess"
"citrix-xendesktop-ec2-provisioning","AmazonS3FullAccess"
"rundeck-read-only-iam-permissions","IAMReadOnlyAccess"
  • wonderfull-it works !! – Milister Mar 9 '18 at 14:19
  • Ah, that looks better than what I attempted. Didn't think of converting the lines individually... – Kusalananda Mar 9 '18 at 14:28
  • @Kusalananda, yes, jq pipes are pretty "flowable" – RomanPerekhrest Mar 9 '18 at 14:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.