Parsing through json data for aws sns event data in python

I think you may need to load the json:

import json

def lambda_handler(event, context):
    message = event['Records'][0]['Sns']['Message']
    parsed_message = json.loads(message)
    print(parsed_message['Records'][0]['s3']['bucket']['name'])

Gives me

u'bucketname'

Or are you doing the loads somewhere outside your function?


I'm trying to do the same thing in node.js. I mistakenly assumed the event was passing JSON and not a string. I added:

var message = event.Records[0].Sns.Message;
console.log('Message received from SNS:', message);
var bleh=JSON.parse(event.Records[0].Sns.Message);
var blah = bleh.Records[0].s3.bucket.name;
console.log('Bucket Name:', blah);

which finally kicked out the right bucket name.