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);
gefragt vor 2 Jahren924 Aufrufe
2 Antworten
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.

beantwortet vor 2 Jahren
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
EXPERTE
iwasa
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen