AWS-SDK V3 Translate DDB Streams Json to Normal Json
Introduction
DynamoDB (DDB) is a nosql storage solution offered by AWS which offers single digit millisecond performance. DDB offers a key value based intefrace where you can store a JSON document. DDB stores object in its internal json format instead of the one requested by the client. AWS-SDK automatically takes care of the translation to and from DDB Json and requested json.
let employee = {
"name": "Alpha",
"age": 20,
"phone": "+1-222-222-1111"
}
DynamoDB json Representation
let employee = {
"name": {
"S": "Alpha",
},
"age": {
"N": "20"
},
"phone": {
"S": "+1-222-222-1111"
}
}
DynamoDB Streams offer an easy mechanism to write reactors to data changes. An INSERT/UPDATE/DELETE action on the Dynamodb table generates a stream event. The event contains keys
and/or object
affected by the action. But the stream includes object represented as DynamoDB json and can be cumbersome to deal with.
Following is utility methods that simplifies translate between the two formats. marshall converts json object to ddb json. unmarshall helps you convert object from ddb json to normal json object.
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.52.0",
"@aws-sdk/lib-dynamodb": "^3.52.0"
}
Sample Implementation for Employee
import { unmarshall , marshall } from "@aws-sdk/util-dynamodb";const ddbJsonToNormal = (input: { [key: string]: AttributeValue }): Employee => {
console.log('Input value ', input);
const parsed = unmarshall(input as any) as Employee;
console.log('Output Parsed Value', parsed); return parsed;
}const normalToDdbJSON = (input : Employee) : {[key:string]: AttributeValue} => {
console.log('Input value', input);
const parsed = marshall(input as any); console.log('Output Parsed value', parsed);
return parsed;
}
Using the above methods helps you convert from and to dynamodb json format. These methods also accept a variety of options to control situations like, property has empty values, etc.
Conclusion
While dynamodb streams are easy and offer a great deal of flexibility in how to post process, data change events. Using the above methods you can easily translate from ddb json to normal json and vice versa.