2

I have below Dictionary those values I am pulling from aws S3 bucket -

{u'Policy': u'{"Version":"2012-10-17","Statement":[{"Sid":"AddPerm","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::elliemaetestbucket1/*"},{"Sid":"AddPerm1","Effect":"Allow","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::elliemaetestbucket1/*"}]}'}

I want to read "Sid" value and compare it with a string that I am getting from my yaml file. Dictionary can have multiple sids but I need to stop where my sid matches with the string that I am pulling from yaml. I am sure I am missing something very simple. But I have tried almost all the solutions most of the time I get unicode object not callable error. Can somebody please provide some direction on how I can access it. I know this would be something very simple but I am sorry I am stuck at this from 2 days.

3
  • You ain't gonna get anywhere without parsing the JSON from your_data["Policy"]. Commented Jul 23, 2017 at 4:01
  • What is the type of the data returned from S3? What line of code gives you that "Not callable" error. If its a unicode string, use json.dumps to create a regular json and then extract "Sid" Commented Jul 23, 2017 at 4:02
  • Data returned from s3 is Dictionary type. it returns Current_policy which consists of above dict that I mentioned Commented Jul 23, 2017 at 4:05

1 Answer 1

3

Your data's Policy key holds a literal JSON, you have to parse it first before you can access its nested fields:

import json

policy = json.loads(your_data["Policy"])

print(policy["Statement"][0]["Sid"])  # Sid of the first Statement
print(policy["Statement"][1]["Sid"])  # Sid of the second Statement
Sign up to request clarification or add additional context in comments.

1 Comment

yayyy I knew it I was doing some stupidity. Thank you so much for your help I really appreciate.

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.