[英]AWS Python Lambda Function JSON format
我在 AWs Lambda 中使用以下 Python 函數:
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
#always start with the lambda_handler
def lambda_handler(event, context):
# make the connection to dynamodb
dynamodb = boto3.resource('dynamodb')
# select the table
table = dynamodb.Table("test")
response = table.query(
KeyConditionExpression=Key('coursename').eq('intro')
)
data = response['Items']
return {'body' : data}
它輸出以下 JSON - 注意“body”鍵? 當我嘗試在我的應用程序中使用響應時,這會產生一些問題,因為我必須引用“正文”作為響應的一部分。
來自 Lambda 的 JSON 響應
{
"body": [{
"coursename": "introto1",
"course-lesson-name": "Welcome to One! "
}, {
"coursename": "introto2",
"course-lesson-name": "What is One?"
}, {
"coursename": "introto2",
"course-lesson-name": "What Can We do with One?"
}]
}
這是我需要 Python 函數輸出的 JSON 格式。 這可以在 AWS Lambda 中完成嗎?
我需要的 JSON 格式:
[{
"coursename": "introto1",
"course-lesson-name": "Welcome to One! "
}, {
"coursename": "introto2",
"course-lesson-name": "What is One?"
}, {
"coursename": "introto2",
"course-lesson-name": "What Can We do with One?"
}]
響應采用該格式,因為您將其顯式包裝在具有body
屬性的 JSON 對象中。 將return {'body' : data}
更改為return data
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.