2

I have an AWS Lambda function that returns this:

{'Records': [{'EventSource': 'aws:sns', 'EventVersion': '1.0', 
'EventSubscriptionArn': 'deleted', 'Sns': {'Type': 'Notification', 'MessageId': 
'ID', 'TopicArn': 'deleted', 'Subject': None, 'Message': '{"addressLength":
{"NULL":true},"lName":{"NULL":true},"zipCode":{"NULL":true},"loanType":
{"S":"Car"},"city":{"NULL":true},"birthDate":{"NULL":true},"loanAmount":
{"N":"100000"},"ssn":{"NULL":true},"emailAddress":
{"S":"[email protected]"},"fName":{"S":"Testy"},"phoneNumber":
{"S":"2220009999"},"streetAddress":{"NULL":true},"LoanBotTableId":
{"S":"85863390"},"state":{"NULL":true}}', 'Timestamp': '2019-09-
24T06:09:37.025Z', 'SignatureVersion': '1', 'Signature': 'deleted',
 'SigningCertUrl': 'URL', 'UnsubscribeUrl': 'URL', 'MessageAttributes': {}}}]}

I am trying to get the Message information. When I use Message = event['Records'][0]['Sns']['Message']

I get:

{
    "addressLength": {
        "NULL": true
    },
    "lName": {
        "NULL": true
    },
    "zipCode": {
        "NULL": true
    },
    "loanType": {
        "S": "Car"
    },
    "city": {
        "NULL": true
    },
    "birthDate": {
        "NULL": true
    },
    "loanAmount": {
        "N": "9000"
    },
    "ssn": {
        "NULL": true
    },
    "emailAddress": {
        "S": "[email protected]"
    },
    "fName": {
        "S": "haad"
    },
    "phoneNumber": {
        "S": "9099999999"
    },
    "streetAddress": {
        "NULL": true
    },
    "LoanBotTableId": {
        "S": "39765985"
    },
    "state": {
        "NULL": true
    }
}

When I use

Message = event['Records'][0]['Sns']['Message']['phoneNumber']['S']

I get an

[ERROR] TypeError: string indices must be integers

Can someone help me get the information?

1
  • [ERROR] TypeError: string indices must be integers Commented Sep 24, 2019 at 6:29

1 Answer 1

3

The error you get is due to the fact that Message = event['Records'][0]['Sns']['Message'] is a string but you try to access a phoneNumber key, hence the error.

First, you should parse the json and then access the data:

message = event['Records'][0]['Sns']['Message']
parsed_message = json.loads(message)
phone_number = parsed_message['phoneNumber']['S']
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.