5

JSON file:

"UserDetailList": [
{
    "UserName": "ec2-provisioning",
    "GroupList": [],
    "CreateDate": "2017-11-07T14:20:14Z",
    "UserId": "1234556",
    "Path": "/",
    "AttachedManagedPolicies": [
        {
            "PolicyName": "EC2FullAccess",
            "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": "read-only-iam-permissions",
    "GroupList": [],
    "CreateDate": "2018-03-09T11:13:38Z",
    "UserId": "AABCDEFGHG6EQ",
    "Path": "/",
    "AttachedManagedPolicies": [
        {
            "PolicyName": "IAMReadOnlyAccess",
            "PolicyArn": "arn:aws:iam::aws:policy/IAMReadOnlyAccess"
        }
    ],
    "Arn": "arn:aws:iam::123456789:user/rundeck-read-only-iam-permissions"
}]

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

I can get

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

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

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"
| improve this answer | |
  • 1
    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
  • 1
    Truly brilliant, and simple enough to understand. There are many solutions out there that look like a 2 hour investment. Thank you. – Merlin Aug 9 '19 at 21:03

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