0

I spent a couple of hours trying to figure out how to insert an array of objects into DynamoDB and could not find a clear answer. After playing with it for a bit, I was able to find a solution. See below. Just an FYI, I wrapped this in a promise, but I did not include that in the code. I hope this helps someone.

1 Answer 1

2
    const AWS = require("aws-sdk");    

    AWS.config.update({
      region: <<Your Regions>>,
      endpoint: <<Your endpoint>>
    });

    var dynamo = new AWS.DynamoDB();

    var params = {
       TableName: <<name of your table>>,
       Item: {
           'PK' : {"S": `<<Your ITEM'S PK>>`},
           'SK' : {"S": `<<YOUR ITEM'S SK`},
           'RecentLocations': {"L": [ //>>>>>>>>>>>>>> ARRAY OF OBJECTS STARTS HERE!!!!
               { //we start the first value here
                   "M": { //we specify it is a map/object
                       "path": {"S": device.oneLocationOnly.path }, //FIRST PROPERTY AND VALUE
                       "locationArray": { //SECOND PROPERTY, WHICH IS AN ARRAY/LIST
                           "L": device.recentLocations.locationArray
                       }
                   }
               }
           ]}
       },
           ConditionExpression: "attribute_not_exists(PK)"
   };

dynamo.putItem(params, function(err, data) {
  if (err) {
      console.log("Error", err);
      resolve([false, err])
  } else {
      resolve([true]);
  }
});
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.