How to insert json in dynamodb
Solution 1
You can use the DynamoDB Document Client SDK:
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.
For your case specifically look at the Map Attribute value (M)
being passed as MapAttribute
in the example below extracted from the official documentation. The Document Client API takes care of the proper marshalling/unmarshalling between the Javascript and DynamoDB types (meaning that you don't have to specify the Attribute Values (S,N,L,...)
as it is required when using the non-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);
});
Solution 2
DynamoDB can store JSON data , also the data format you wish to insert.
There are two methods which can be used to format the data while inserting into DynamoDB and also while retrieving the DB data into desired JSON method.
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html
These methods are : input
and output
.
I have shared two sample snippets of AWS lambda code to do so, hope this solves your problem.
With the following way you can convert JSON data into DynamoDB supported format and then insert it:
const AWS = require('aws-sdk');
//setting the region
AWS.config.update({region: 'ap-south-1'});
// Create the DynamoDB service object
const ddb = new AWS.DynamoDB.DocumentClient();
const getParams = (updatedTasks)=>{
const params = {
TableName: 'todo-application',
Key: {
"userID" : "[email protected]",
},
UpdateExpression: "set tasks = :updatedTasks",
ExpressionAttributeValues:{
":updatedTasks":updatedTasks
},
ReturnValues:"UPDATED_NEW"
};
return params;
}
const dbQuery =(newTask)=>{
let updatedTask = AWS.DynamoDB.Converter.input(newTask, true);
console.log('updated task formatted ',updatedTask);
let params = getParams(updatedTask);
return new Promise((resolve,reject) =>{
ddb.update(params, function(err, data) {
if (err) {
console.log("Error", err);
reject(err);
} else {
console.log("Success", data);
resolve(data.Item)
}
});
});
}
exports.updateTodoJobsOfAUser = async (event) => {
const insertObject = [
{
statement:'learn Dynamo DB', id: 23
},
{
statement:'Learn Serverless Architecture',id:24
}]
const data = await dbQuery(insertObject);
const response = {
statusCode: 200,
body: JSON.stringify(data)
};
return response;
};
With following way you can convert DynamoDB data into JSON format :
const dbQuery =()=>{
return new Promise((resolve,reject) =>{
ddb.get(params, function(err, data) {
if (err) {
console.log("Error", err);
reject(err);
} else {
console.log("Success", data);
resolve(data.Item)
}
});
});
}
exports.getAllTodoJobsOfAUser = async (event) => {
const data = await dbQuery();
const formattedData = AWS.DynamoDB.Converter.output(data, true);
const response = {
statusCode: 200,
body: JSON.stringify(formattedData)
};
return response;
};
It works for me. Happy to help !! :)
mks
Updated on December 11, 2020Comments
-
mks over 3 years
Here's my code.
I want to insertasset_data
json intoasset_data
column. i am using aws sdk. It says aws sdk now has support for json. http://aws.amazon.com/releasenotes/SDK/JavaScript/1691866671551861var 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 });
-
Gopala almost 8 yearsIs there a way to use this DocumentClient() thing from CLI to do a put-item? Can't see it in documentation.Thanks!