I am using following code to fetch data from the DynamoDB.
async fetchData(params: QueryParams) {
return await this.docClient.send(new QueryCommand(params));
}
const dbObject: QueryParams = {
TableName: process.env.TABLE_NAME,
KeyConditionExpression: "PK = :id",
FilterExpression: "ACTIVE = :active",
ExpressionAttributeValues: {
":id": { S: id },
":active": { S: "Y" },
},
ProjectionExpression:
"FUND_MANAGER, FUND_NAME,PRICE_DECIMAL ,ANNUALIZED_YIELD,YTD_GROWTH, AUM_VALUE",
};
I am currently getting results as follows. (Example, not actual response)
{
"Id": {
"S": "456"
},
"Price": {
"N": "650"
},
"ProductCategory": {
"S": "Sporting Goods"
} }
How can I get responses directly as Strings or Numbers, Like this directly
{ Id : "456", Price : 650 }
2
Answers
You could try transforming the data with something like this:
Do note, I am assuming that the DynamoDB response follows the structure you provided, where each attribute has a single key-value pair representing its type and value. If your actual response structure is different, you’ll need to change the code but this should give you an idea of how you might approach it.
Assuming you’ve set your client to match you naming convention, you’re using the document client which provides native JSON abstraction by default, they query your shared wouldn’t actually work. Try this, and you will obtain the data as you desire: