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);
feita há 2 anos924 visualizações
2 Respostas
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.

respondido há 2 anos
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
ESPECIALISTA
iwasa
respondido há 2 anos

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas