1

i am new in Python language. I need to get all Amazon-Web-Services Identity and Access Management (Amazon-IAM) policy details using Boto 3 and Python.

I tried to parse JSON output from Boto 3 client and also need to save key-value pair into a map (policyName, Arn). Sample JSON output is like this:

{
    'ResponseMetadata': {
        'HTTPStatusCode': 200,
        'HTTPHeaders': {
            'vary': 'Accept-Encoding',
            'content-length': '19143',
            'content-type': 'text/xml',
            'date': 'Thu, 23 Feb 2017 06:39:25 GMT'
        }
    },
    u 'Books': [ {
        u 'PolicyName': 'book1',
        u 'Arn': '002dfgdfgdfgdfgvdfxgdfgdfgdfgfdg',
        u 'CreateDate': datetime.datetime(2017, 2, 22, 13, 10, 55, tzinfo = tzutc()),
        u 'UpdateDate': datetime.datetime(2017, 2, 22, 13, 10, 55, tzinfo = tzutc())
    }, {
        u 'PolicyName': 'book2','
        u 'Arn': '002dfgdfgdfgdfgvdfxgdfgdfgdfgfdg',
        u 'CreateDate': datetime.datetime(2017, 2, 22, 13, 10, 55, tzinfo = tzutc()),
        u 'UpdateDate': datetime.datetime(2017, 2, 22, 13, 10, 55, tzinfo = tzutc())
    }]
}

I have following code

iampolicylist_response = iamClient.list_policies(
    Scope='Local',
    MaxItems=150
)
    print iampolicylist_response
    res=json.dumps(iampolicylist_response)
print res
ret={}
for i in res["PolicyName"]:
  ret[i["PolicyName"]]=i["Arn"]
return ret  

Using json.loads, it shows error like this

TypeError: expected string or buffer

Using json.dumps, it shows error like this

TypeError: datetime.datetime(2017, 2, 22, 13, 10, 55, tzinfo=tzutc()) is not JSON serializable

What is actual issue?

1
  • res=json.dumps(iampolicylist_response) will convert the object to json string. Commented Feb 23, 2017 at 7:19

1 Answer 1

1

The result iampolicylist_response is already a dictionary

You do not need to parse it .

See http://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.list_policies

The response is a dictionary object

Remove res=json.dumps(iampolicylist_response)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.