如何在从sns接收json数据的Python中读取json数据

blmhpbnm  于 2022-12-01  发布在  Python
关注(0)|答案(1)|浏览(136)

这是我从aws sns通知接收的json数据。我想访问Records->Sns->Message中的deploymentGroupName
在我的lambda python代码中,我尝试这样做。

eventName = json.loads(event.Records[0].Sns.Message).deploymentGroupName;

这是我收到的json。

{
    'Records': [{
        'EventSource': 'aws:sns',
        'EventVersion': '1.0',
        'EventSubscriptionArn': 'arn:aws:sns:us-east-1:1236542:project-Deploy-Success:123-654-12-b177-123654',
        'Sns': {
            'Type': 'Notification',
            'MessageId': '6ef313fa-46d2-5841-b162-4805edfb421c',
            'TopicArn': 'arn:aws:sns:us-east-1:428219256379:project-Deploy-Success',
            'Subject': 'SUCCEEDED: AWS CodeDeploy d-E8BYQ65CL in us-east-1 to project-code-deploy',
            'Message': '{"region":"us-east-1","accountId":"213321213321","eventTriggerName":"Sandbox-Deployment-Triggered","applicationName":"project-code-deploy","deploymentId":"d-E8BYQ65CL","deploymentGroupName":"Sandbox-ec2-deployment","createTime":"Tue Nov 29 06:38:20 UTC 2022","completeTime":"Tue Nov 29 06:38:33 UTC 2022","deploymentOverview":"{\\"Succeeded\\":1,\\"Failed\\":0,\\"Skipped\\":0,\\"InProgress\\":0,\\"Pending\\":0}","status":"SUCCEEDED"}',
            'Timestamp': '2022-11-29T06:38:33.558Z',
        }
    }]
}

现在马上给出这个错误。
[错误]名称错误:名称'json'未定义Traceback(最近的呼叫在最后):文件“/var/task/lambda_function.py”,第12行,在lambda_handler中,事件名称= json.loads(事件.记录[0].Sns.消息)。部署组名称;

t3irkdon

t3irkdon1#

如果event没有""环境,则它已经转换为dict。但是,您需要为Message处理json

import json

msg = event['Records'][0]['Sns']['Message']
deploymentGroupName = json.loads(msg)['deploymentGroupName']
deploymentGroupName

输出:

'Sandbox-ec2-deployment'

相关问题