How to insert json in dynamodb

Here is my code.
I want to insert asset_data json in the asset_data column. I am using aws sdk. He says aws sdk now supports json. http://aws.amazon.com/releasenotes/SDK/JavaScript/1691866671551861

 var asset_data = { "name": "name" + i, "contentUrl": "http://www.hdwallpapersimages.com/nature-beauty-desktop-images/94892/", "size": 300, "headline": "headline", "description": "assetUrl reference for the creator", "encodingFormat": 'jpeg' }; var params = { TableName: 'xyz', Item: { // a map of attribute name to AttributeValue "asset_id": {S: "asset" + i}, "hit_id": {S: "0"}, "created_date": {"S": Date.now().toString()}, "status": {N: "0"}, "operation": {S: "image_tagging"}, "asset_data": {L: asset_data}, "source": {S: "DAM"}, "completed_date": {S: Date.now().toString()}, "response_data": {S: "taged value"} // more attributes... }, ReturnValues: 'NONE', // optional (NONE | ALL_OLD) ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES) ReturnItemCollectionMetrics: 'NONE' // optional (NONE | SIZE) }; db.putItem(params, function (err, data) { if (err) console.log(err); // an error occurred else console.log("inserted..."); // successful response }); 
+6
amazon-web-services amazon-dynamodb aws-sdk
source share
1 answer

You can use the DynamoDB client client SDK:

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property

The document client simplifies working with elements in Amazon DynamoDB by abstracting the concept of attribute values. This abstraction annotates the native JavaScript types that are included as input parameters, since it also converts the annotated response data into native JavaScript types.

In your case, specifically look at the value of the map attribute (M) , which is passed as MapAttribute in the example below, extracted from the official documentation. The Client Client API takes care of proper sorting / disassembling between the Javascript and DynamoDB types (this means that you do not need to specify Attribute Values (S,N,L,...) , as required when using the document-based SDK):

 var params = { TableName = 'Table', Item: { HashKey: 'haskey', NumAttribute: 1, BoolAttribute: true, ListAttribute: [1, 'two', false], MapAttribute: { foo: 'bar'}, NullAttribute: null } }; var docClient = new AWS.DynamoDB.DocumentClient(); docClient.put(params, function(err, data) { if (err) console.log(err); else console.log(data); }); 
+8
source share

All Articles