24

通过 DynamoDB流事件给定一些DynamoDB JSONNewImage,我如何将其解组为常规 JSON

{"updated_at":{"N":"146548182"},"uuid":{"S":"foo"},"status":{"S":"new"}}

通常我会使用AWS.DynamoDB.DocumentClient,但是我似乎找不到通用的 Marshall/Unmarshall 函数。

旁注:我是否会丢失任何将 DynamoDB JSON解组为 JSON 并再次返回的内容?

4

2 回答 2

39

您可以使用该AWS.DynamoDB.Converter.unmarshall功能。调用以下将返回{ updated_at: 146548182, uuid: 'foo', status: 'new' }

AWS.DynamoDB.Converter.unmarshall({
    "updated_at":{"N":"146548182"},
    "uuid":{"S":"foo"},
    "status":{"S":"new"}
})

可以在 DynamoDB 的编组 JSON 格式中建模的所有内容都可以安全地与 JS 对象相互转换。

于 2017-06-14T06:01:33.887 回答
15

适用于 JavaScript 版本 3 (V3) 的 AWS 开发工具包提供了可靠地编组和解组DynamoDB记录的好方法。

const { marshall, unmarshall } = require("@aws-sdk/util-dynamodb");

const dynamo_json = { "updated_at": { "N": "146548182" }, "uuid": { "S": "foo" }, "status": { "S": "new" } };

const to_regular_json = unmarshall(dynamo_json);

const back_to_dynamo_json = marshall(to_regular_json);

输出:

// dynamo_json    
{ 
      updated_at: { N: '146548182' },
      uuid: { S: 'foo' },
      status: { S: 'new' }
}

// to_regular_json
{ updated_at: 146548182, uuid: 'foo', status: 'new' }

// back_to_dynamo_json
{
   updated_at: { N: '146548182' },
   uuid: { S: 'foo' },
   status: { S: 'new' }
}
于 2021-02-03T05:31:38.867 回答