My goal is to recursively parse a DynamoDB
request from dynamo.getItem
method.
Unfortunately, I can't find similar method in DynamoDB SDK for Node.js.
.html
I am looking for an effective way of removing types from the result. In DynamoDB, keys are one-letter names of properties, like "N", "S", "M".
Description of these types:
N means DynamoDB Number type
S means dynamoDB String type
M means DynamoDB Map type (object with properties)
Current JSON structure is:
{
"id":{
"N":"4"
},
"number":{
"N":"1"
},
"data":{
"M":{
"aaa":{
"S":"AAA"
},
"lv2":{
"M":{
"lv3":{
"M":{
"ccc":{
"N":"111"
}
}
},
"bbb":{
"S":"BBB"
}
}
}
}
}
}
I need a JavaScript function that maps the above JSON into a shorter version:
{
"id": "4",
"number": "1",
"data": {
"aaa": "AAA",
"lv2": {
"lv3": {
"ccc": "111"
},
"bbb": "BBB"
}
}
}
It is generally a JavaScript question, but I suppose folks with DynamoDB experience might know how to deal with this issue.
Any ideas?
My goal is to recursively parse a DynamoDB
request from dynamo.getItem
method.
Unfortunately, I can't find similar method in DynamoDB SDK for Node.js.
http://docs.aws.amazon./AWSJavaScriptSDK/latest/AWS/DynamoDB.html
I am looking for an effective way of removing types from the result. In DynamoDB, keys are one-letter names of properties, like "N", "S", "M".
Description of these types:
N means DynamoDB Number type
S means dynamoDB String type
M means DynamoDB Map type (object with properties)
Current JSON structure is:
{
"id":{
"N":"4"
},
"number":{
"N":"1"
},
"data":{
"M":{
"aaa":{
"S":"AAA"
},
"lv2":{
"M":{
"lv3":{
"M":{
"ccc":{
"N":"111"
}
}
},
"bbb":{
"S":"BBB"
}
}
}
}
}
}
I need a JavaScript function that maps the above JSON into a shorter version:
{
"id": "4",
"number": "1",
"data": {
"aaa": "AAA",
"lv2": {
"lv3": {
"ccc": "111"
},
"bbb": "BBB"
}
}
}
It is generally a JavaScript question, but I suppose folks with DynamoDB experience might know how to deal with this issue.
Any ideas?
Share Improve this question edited Oct 30, 2015 at 20:46 Xakep 631 gold badge2 silver badges10 bronze badges asked Jan 15, 2015 at 15:28 Cyprian GepfertCyprian Gepfert 3832 gold badges4 silver badges15 bronze badges6 Answers
Reset to default 3aws-sdk now offers support to marshall/unmarshall data with AWS.DynamoDB.Converter
.
See docs here:
https://docs.aws.amazon./AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html
Update 1/7/2018: This converter code is built into AWS.DynamoDB.DocumentClient
so there's no need to use Converter.marshall
and Converter.Unmarshall
directly. See:
https://docs.aws.amazon./AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html
If you're using node, there's a cool package that does the dynamo-to-json marshalling and unmarshalling that worked great for me:
https://www.npmjs./package/dynamodb-marshaler
Basic Marshaling of Objects
var AWS = require('aws-sdk');
var marshalItem = require('dynamodb-marshaler').marshalItem;
AWS.config.region = 'us-west-2';
var dynamoDb = new AWS.DynamoDB();
dynamoDb.putItem({
TableName: 'users',
Item: marshalItem({username: 'nackjicholson'}) // {username: {S: 'nackjicholson'}}
});
Basic Unmarshaling of Objects
var AWS = require('aws-sdk');
var unmarshalItem = require('dynamodb-marshaler').unmarshalItem;
AWS.config.region = 'us-west-2';
var dynamoDb = new AWS.DynamoDB();
var data = dynamoDb.scan({
TableName: 'users'
}, function(err, data) {
// data.Items = [{username: {S: 'nackjicholson'}]
var items = data.Items.map(unmarshalItem);
console.log(items); // [{username: 'nackjicholson'}]
});
If you're using @aws-sdk/client-dynamodb (npm), then you can pair it with the unmarshall
method from @aws-sdk/util-dynamodb.
For example:
const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb");
const { marshall, unmarshall } = require("@aws-sdk/util-dynamodb");
(async () => {
const client = new DynamoDBClient({ region: "eu-west-2" });
const params = {
Key: marshall({
HashKey: "KeyName",
}),
TableName: "my-table",
};
const mand = new GetItemCommand(params);
const { Item } = await client.send(mand);
const POJO = unmarshall(Item); // POJO is without types :)
})();
Answering for this question very late, Hope it will help others in future.
AWS provides schema based library for DynamoDb, It supports data mapper, data mapper annotation, projection expression, data marshaling and un marshaling.
Amazon DynamoDB DataMapper For JavaScript
Sample Snippet
import {
attribute,
autoGeneratedHashKey,
rangeKey,
table,
versionAttribute,
} from '@aws/dynamodb-data-mapper-annotations';
@table('my_table')
class MyDomainClass {
@autoGeneratedHashKey()
id: string;
@rangeKey({defaultProvider: () => new Date()})
createdAt: Date;
@versionAttribute()
version: number;
@attribute()
toggle?: boolean;
@attribute({memberType: 'String'})
tags?: Set<string>;
// This property will not be saved to DynamoDB.
notPersistedToDynamoDb: string;
}
// fetch an object
const toGet = new MyDomainClass();
toGet.id = 'ID_TO_FETCH';
const fetched = await mapper.get(toGet);
Other option is create your custom map:
const items = data.Items.map(
(item) => {
return {
id: item.id.N,
number: item.number.N,
data: {
aaa: item.data.M.aaa.S,
lv2: {
lv3: {
ccc: item.data.M.lv2.M.lv3.Mc.N
},
bbb: item.data.M.lv2.M.bbb.S
}
}
}
}
);
You can parse dynamodb
data using two methods.
one is parsing without using mapping function.
{
roll no: 10
Id: 2
Name: {
"S": "Ankit"
}
empId:{
"N": "152"
}
edu: {
"S": "postGraduate"
}
Add:{
"S":"Delhi"
}
phone: {
"N": "941354"
}
nationality: {
"S": "India"
}
}
We can simply unmarshal
such data with AWS.Dynamodb.Converter.Unmarshal
.
const result = {
...AWS.DynamoDB.Converter.unmarshall(message),
...message.description,
primary_key: message.primary_key,
sort_key: message.sort_key,
};
return {
body: JSON.stringify(result)
}