0

I have a JSON having an array of objects in the following format. How can i add it in dynamoDB using a lambda function(NodeJS). Assume my JSON is in an s3 bucket name "databucket" and the file name is data.json. I have used "userId" as the key while creating the DB table. Thanks in advance

{
  "records": [
    {
      "first-name": "Mark",
      "last-name": "rob",
      "userId": "fvdvfd"
    },
    {
      "first-name": "Alan",
      "last-name": "thomas",
      "userId": "wee"
    },
    {
      "first-name": "Joan",
      "last-name": "Young",
       "userId": "wee"
    }
]
}

1 Answer 1

1
  1. First, you have to retrieve the data.json from S3 bucket and read it's content into a variable.
  2. Secondly, you should create dynamodb parameter to provide to the dynamodb client PUT method. Since it is an array of objects you have to loop through the array and put each object or else you can use batchWrite method in dynamodb as follows,

    var AWS = require('aws-sdk'); var insertBatchRecords = function(callback) { var dynamodb = new AWS.DynamoDB(); var bulkInsertParams = { RequestItems: { "Records": [ { PutRequest: { Item: { "first-name": "Mark", "last-name": "rob", "userId": "fvdvfd" } }}, { PutRequest: { Item: { "first-name": "Alan", "last-name": "thomas", "userId": "wee" } }}] } }; dynamodb.batchWriteItem(bulkInsertParams, callback); }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.