How to insert json in dynamodb

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 !! :)


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);
});