简体   繁体   English

解析 AWS Lambda JSON

[英]Parse AWS Lambda JSON

I have an AWS Lambda function that returns this:我有一个AWS Lambda function 返回这个:

{'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":"test@testerson.com"},"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']当我使用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": "had@g.com"
    },
    "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 [错误] TypeError:字符串索引必须是整数

Can someone help me get the information?有人可以帮我获取信息吗?

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.您收到的错误是由于Message = event['Records'][0]['Sns']['Message']是一个字符串,但您尝试访问phoneNumber键,因此出现错误。

First, you should parse the json and then access the data:首先,您应该解析 json 然后访问数据:

message = event['Records'][0]['Sns']['Message']
parsed_message = json.loads(message)
phone_number = parsed_message['phoneNumber']['S']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM