在 Typescript 和 AWS Lambda 中将 DynamoDB 数据格式化为普通 JSON

1 unmarshalling amazon-web-services amazon-dynamodb

我正在使用查询从 DynamoDB 检索数据,并返回以下内容:

[{"serviceUserId":{"S":"123456789"},"createdDate":{"S":"11-12-2021"}}]

DynamoDB JSON 格式具有我试图通过转换为普通 JSON 格式来摆脱的类型。我尝试过使用AWS.DynamoDB.Converter.unmarshall但我的代码中出现错误:

Argument of type 'ItemList' is not assignable to parameter of type "AttributeMap".
  Index signature for type 'string' is missing in type "AttributeMap[]".
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

                 if (result.Count > 0) {
                     const newImage = AWS.DynamoDB.Converter.unmarshall(
                        result.Items
                         )
                   console.log('new Image: ' + JSON.stringify(newImage));
                    resolve(newImage);
                 } else { 
                     console.log('No record found');
                     reject(err);
                 }
Run Code Online (Sandbox Code Playgroud)

如果我删除 DynamoDB JSON 中的 [] 括号,那么它会成功转换,但显然我无法在程序中执行此操作,因为括号的存在是有原因的!

unmarshall有谁知道如何将我的 JSON 文件转换为可接受的格式?

fed*_*nev 7

映射所有项目并一一整理。 (un)marshal 接受对象类型,而不是数组。

import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';  // SDK V3, but worked the same in V2

(() => {
  const items = [{ serviceUserId: { S: '123456789' }, createdDate: { S: '11-12-2021' } }];
  
  // from DynamoDB JSON
  const unmarshalled = items.map((i) => unmarshall(i));

  // make the return trip back to DynamoDB JSON
  const marshalled = unmarshalled.map((i) => marshall(i));
})();
Run Code Online (Sandbox Code Playgroud)