如何格式化来自 DynamoDB Scan() 的响应 API (Python)
How to format response from DynamoDB Scan() API (Python)
我正在进行 API 网关练习,但我无法格式化来自 DynamoDB 的响应。这是我得到的回复:
{'Items': [{'Date': Decimal('3022020'), 'ResultID': Decimal('32'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}, {'Date': Decimal('3022020'), 'ResultID': Decimal('31'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}, {'Date': Decimal('3022020'), 'ResultID': Decimal('29'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}, {'Date': Decimal('3022020'), 'ResultID': Decimal('30'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}], 'Count': 4, 'ScannedCount': 11, 'ResponseMetadata': {'RequestId': 'MP2RQ0V4QT898T10DVMMJVMMVRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Sun, 06 Dec 2020 18:29:46 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '487', 'connection': 'keep-alive', 'x-amzn-requestid': 'MP2RQ0V4QT898T10DVMMJVMMVRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '972061714'}, 'RetryAttempts': 0}}
对我来说,它看起来像一个嵌套数组,但每当我尝试使用响应对象时,它都会抛出内部服务器错误。本质上,lambda 正在扫描数据库中的两个属性(联赛和球队),并将结果 returns 作为强制字符串的响应返回给浏览器。下面是我使用那个未格式化的字符串对象的要点。
dbresponse = table.scan(FilterExpression=....)
responseObject = {
'statusCode': 200
'body': str(dbresponse)
}
return responseObject
我想要一些来自 DynamoDB 的 'body':str(dbresponse)
格式的响应,但我不太确定如何去做。
谢谢!
编辑
看来我还没有像我想的那样真正掌握 'helper method' 的概念(见下面的回答)。
我在 lambda_handler 默认方法中添加了我的辅助方法的非工作版本。我遇到了一些 NameErrors,我假设我传递的对象有误,或者调用数组中的属性或其他错误。
def json_response(respDB):
for response in respDB['Items']:
response = response + { Date: respDB.Date,
League: respDB.League, Team: respDB.Team,
Score: respDB.Score }
return response
此外,按照建议,我正在使用此方法并按照建议将数据库响应变量作为 'body': json_response(respDB)
传递。
感谢您的帮助!
您需要自己在 JSON 中构建响应主体。尝试创建一个辅助方法,该方法接收 DynamoDB 的输出并以 JSON.
格式输出
辅助方法应如下所示:
def json_response(respDB):
response = []
for item in respDB['Items']:
response.append( { 'Date': item['Date'], 'League': item['League'], 'Team': item['Team']})
return response
并使用它来构建格式正确的响应正文
dbresponse = table.scan(FilterExpression=....)
responseObject = {
'statusCode': 200
'body': format_response(dbresponse)
}
return responseObject
我正在进行 API 网关练习,但我无法格式化来自 DynamoDB 的响应。这是我得到的回复:
{'Items': [{'Date': Decimal('3022020'), 'ResultID': Decimal('32'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}, {'Date': Decimal('3022020'), 'ResultID': Decimal('31'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}, {'Date': Decimal('3022020'), 'ResultID': Decimal('29'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}, {'Date': Decimal('3022020'), 'ResultID': Decimal('30'), 'Team': 'Roma', 'League': 'SerieA', 'Result': '3-1'}], 'Count': 4, 'ScannedCount': 11, 'ResponseMetadata': {'RequestId': 'MP2RQ0V4QT898T10DVMMJVMMVRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Sun, 06 Dec 2020 18:29:46 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '487', 'connection': 'keep-alive', 'x-amzn-requestid': 'MP2RQ0V4QT898T10DVMMJVMMVRVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '972061714'}, 'RetryAttempts': 0}}
对我来说,它看起来像一个嵌套数组,但每当我尝试使用响应对象时,它都会抛出内部服务器错误。本质上,lambda 正在扫描数据库中的两个属性(联赛和球队),并将结果 returns 作为强制字符串的响应返回给浏览器。下面是我使用那个未格式化的字符串对象的要点。
dbresponse = table.scan(FilterExpression=....)
responseObject = {
'statusCode': 200
'body': str(dbresponse)
}
return responseObject
我想要一些来自 DynamoDB 的 'body':str(dbresponse)
格式的响应,但我不太确定如何去做。
谢谢!
编辑
看来我还没有像我想的那样真正掌握 'helper method' 的概念(见下面的回答)。
我在 lambda_handler 默认方法中添加了我的辅助方法的非工作版本。我遇到了一些 NameErrors,我假设我传递的对象有误,或者调用数组中的属性或其他错误。
def json_response(respDB):
for response in respDB['Items']:
response = response + { Date: respDB.Date,
League: respDB.League, Team: respDB.Team,
Score: respDB.Score }
return response
此外,按照建议,我正在使用此方法并按照建议将数据库响应变量作为 'body': json_response(respDB)
传递。
感谢您的帮助!
您需要自己在 JSON 中构建响应主体。尝试创建一个辅助方法,该方法接收 DynamoDB 的输出并以 JSON.
格式输出辅助方法应如下所示:
def json_response(respDB):
response = []
for item in respDB['Items']:
response.append( { 'Date': item['Date'], 'League': item['League'], 'Team': item['Team']})
return response
并使用它来构建格式正确的响应正文
dbresponse = table.scan(FilterExpression=....)
responseObject = {
'statusCode': 200
'body': format_response(dbresponse)
}
return responseObject