DynamoDB Json 使用 Lambda 格式化 Node.js
DynamoDB Json Formatting w/ Node.js with Lambda
我在格式化我的 json 时遇到了问题,我不知道该怎么做。当我通过 Lambda 运行 这段代码时:
let body;
let statusCode = 200;
const headers = {
"Content-Type": "application/json"
};
body = await dynamo.scan({ TableName: "MyTutorials" }).promise();
body = JSON.stringify(body);
它 returns 作为 json 字符串,看起来像这样:
{
"Items": [
{
"published": false,
"description": "This is an orange.",
"id": 2,
"title": "Orange"
},
{
"published": true,
"description": "this is an apple",
"id": 1,
"title": "Apple"
}
],
"Count": 2,
"ScannedCount": 2
}
太棒了,但是对于我的 React 应用程序,我需要一些更干净的东西,如下所示:
[
{
"published": false,
"description": "This is an orange.",
"id": 2,
"title": "Orange"
},
{
"published": true,
"description": "this is an apple",
"id": 1,
"title": "Apple"
}
]
基本上,我只需要 json 中的直接数组,而不是包含所有其他内容,如“Items”和“Count”。关于如何拨打电话有什么想法吗?
你只需要return正文的项目:
body = JSON.stringify(body.Items);
我在格式化我的 json 时遇到了问题,我不知道该怎么做。当我通过 Lambda 运行 这段代码时:
let body;
let statusCode = 200;
const headers = {
"Content-Type": "application/json"
};
body = await dynamo.scan({ TableName: "MyTutorials" }).promise();
body = JSON.stringify(body);
它 returns 作为 json 字符串,看起来像这样:
{
"Items": [
{
"published": false,
"description": "This is an orange.",
"id": 2,
"title": "Orange"
},
{
"published": true,
"description": "this is an apple",
"id": 1,
"title": "Apple"
}
],
"Count": 2,
"ScannedCount": 2
}
太棒了,但是对于我的 React 应用程序,我需要一些更干净的东西,如下所示:
[
{
"published": false,
"description": "This is an orange.",
"id": 2,
"title": "Orange"
},
{
"published": true,
"description": "this is an apple",
"id": 1,
"title": "Apple"
}
]
基本上,我只需要 json 中的直接数组,而不是包含所有其他内容,如“Items”和“Count”。关于如何拨打电话有什么想法吗?
你只需要return正文的项目:
body = JSON.stringify(body.Items);