DynamoDB result is not structured the way I want

0

Hello,

I am doing a simple command to list all the items in my table. However, the data I am getting back is not structured the way I want. I want a simple JSON structure but DynamoDB is turning the results into nested objects.

DynamoDB gives me below response:

/// What I am currently getting
[
  {
    id: { S: '8' },
    lastName: { S: 'Perry' },
    firstName: { S: 'Matthew' }
  },
  {
    id: { S: '3' },
    firstName: { S: 'Joan' },
    lastName: { S: 'Peter' }
  }
]

But I want this:

/// What I want
[
  {
    id: 8
    lastName:  'Perry' ,
    firstName: 'Matthew' 
  },
  {
    id: 3,
    firstName: 'Joan' ,
    lastName:  'Peter' 
  }
]

How can I achieve the later result set. Below is my code:

const { ExecuteStatementCommand } = require('@aws-sdk/client-dynamodb') 
const { ddbDocClient, memberTableName } = require('./client.js')

const selectAll = async () => {
    const params = {
        Statement: `SELECT * FROM ${memberTableName}`,
        Parameters: [{ S: '3' }]
    }
    console.log(params)
    return await ddbDocClient.send(new ExecuteStatementCommand(params));
}


selectAll()
.then(d => console.log(d.Items))

ddbDocClient was created like this:

 const ddbDocClient = DynamoDBDocumentClient.from(ddbClient);
已提问 2 年前924 查看次数
2 回答
1

This is because you are using the low level client. You need to use the document client or use the unmarshalling util. I think you'll be much happier with the document client. Here is an example of what you want to do.

已回答 2 年前
0

Hi, @GilbertS

You can do martial / unmarshal between native JSON and DynamoJSON by using the "@aws-sdk/util-dynamodb" module.

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_util_dynamodb.html#unmarshall-1

profile picture
专家
iwasa
已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容