简体   繁体   English

Python AWS Lambda 事件 JSON

[英]Python AWS Lambda Event JSON

I am trying to parse event data of AWS Lambda, I have connected it to SQS and I am sending the JSON format using SQS.我正在尝试解析 AWS Lambda 的事件数据,我已将其连接到 SQS 并使用 SQS 发送 JSON 格式。

This is my AWS Lambda Function这是我的 AWS Lambda 函数

import json

def lambda_handler(event, context):
    # print(event)
    # print(event['Records'][0])
    x = event['Records'][0]['body']
    print(x)
    print(type(x))

Following is the event data以下是事件数据

{
   "Records":[
      {
         "messageId":"916f5e95-b2f6-4148-9c62-2ac8e764f06c",
         "receiptHandle":"AQEBmLuoGWtLtFFgvyCFdSPMJh2HKgHOIPWNUq22EOwCzGT8iILZm97CE6j4J6oR71ZpDr3sgxQcJyVZ+dmmvGl+fFftT9GCJqZYrjMGsR2Q6WsMd8ciI8bTtDXyvsk8ektd7UGfh4gxIZoFp7WUKVRcMEeBkubKd8T4/Io81D0l/AK7MxcEfCj40vWEsex1kkGmMRlBtdSeGyy7fJgUq5CFAYWciiWtbSit8S0Y38xZPmsIFhoxP0egQRoJcW4aUgMi469Gj5+khizetybtgC8vux5NCg/IejxcCueXkQ7LKVF8kfRdqRSUYB6DsOrGgfmZpK4wpXIarByNz0R2p7J88meYpj2IVULv/emXsSYaKG4rXnpbH4J9ijbLWckYLAd7wPDzCYri1ZSTgAz0kchsEw==",
         "body":"{\n\"name\": \"aniket\",\n\"tag\": \"hello\"\n}",
         "attributes":{
            "ApproximateReceiveCount":"1",
            "SentTimestamp":"1602046897707",
            "SenderId":"AIDAR3BXDV4FCWXL56NUU",
            "ApproximateFirstReceiveTimestamp":"1602046897712"
         },
         "messageAttributes":{
            
         },
         "md5OfBody":"98da683a47692b39c1d43bd4fa21ed89",
         "eventSource":"aws:sqs",
         "eventSourceARN":"arn:aws:sqs:ap-south-1:126817120010:documentation",
         "awsRegion":"ap-south-1"
      }
   ]
}
    

I am trying to access the body of the data.我正在尝试访问数据正文。

this is what I am getting这就是我得到的

"{\n\"name\": \"aniket\",\n\"tag\": \"hello\"\n}"

And it's type is string.它的类型是字符串。

What do I need to do convert it into a proper JSON format?我需要做什么才能将其转换为正确的 JSON 格式?

I also tried the following:我还尝试了以下方法:

import json

def lambda_handler(event, context):
    data = json.dumps(event['Records'][0]['body'])
    print(data)

This is the output这是输出

"{\n\"name\": \"aniket\",\n\"tag\": \"hello\"\n}"

But this time the type is JSON.但这次的类型是 JSON。

The expected format is预期的格式是

{
"name": "aniket",
"tag": "hello"
}

You have to use json.loads not json.dumps .你必须使用json.loads而不是json.dumps

Try this:尝试这个:

import json

event = {
   "Records":[
      {
         "messageId":"916f5e95-b2f6-4148-9c62-2ac8e764f06c",
         "receiptHandle":"AQEBmLuoGWtLtFFgvyCFdSPMJh2HKgHOIPWNUq22EOwCzGT8iILZm97CE6j4J6oR71ZpDr3sgxQcJyVZ+dmmvGl+fFftT9GCJqZYrjMGsR2Q6WsMd8ciI8bTtDXyvsk8ektd7UGfh4gxIZoFp7WUKVRcMEeBkubKd8T4/Io81D0l/AK7MxcEfCj40vWEsex1kkGmMRlBtdSeGyy7fJgUq5CFAYWciiWtbSit8S0Y38xZPmsIFhoxP0egQRoJcW4aUgMi469Gj5+khizetybtgC8vux5NCg/IejxcCueXkQ7LKVF8kfRdqRSUYB6DsOrGgfmZpK4wpXIarByNz0R2p7J88meYpj2IVULv/emXsSYaKG4rXnpbH4J9ijbLWckYLAd7wPDzCYri1ZSTgAz0kchsEw==",
         "body":"{\n\"name\": \"aniket\",\n\"tag\": \"hello\"\n}",
         "attributes":{
            "ApproximateReceiveCount":"1",
            "SentTimestamp":"1602046897707",
            "SenderId":"AIDAR3BXDV4FCWXL56NUU",
            "ApproximateFirstReceiveTimestamp":"1602046897712"
         },
         "messageAttributes":{

         },
         "md5OfBody":"98da683a47692b39c1d43bd4fa21ed89",
         "eventSource":"aws:sqs",
         "eventSourceARN":"arn:aws:sqs:ap-south-1:126817120010:documentation",
         "awsRegion":"ap-south-1"
      }
   ]
}

parsed = json.loads(event['Records'][0]['body'])
print(json.dumps(parsed, indent=4, sort_keys=True))

Output:输出:

{
    "name": "aniket",
    "tag": "hello"
}

Try using json.loads(string) to deserialize the json.尝试使用 json.loads(string) 反序列化 json。

Also, I don't believe you need to specify the index [0] since 'body' is an object and not an array.另外,我认为您不需要指定索引 [0],因为“body”是一个对象而不是数组。

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

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