Formatting DynamoDB data to normal JSON in AWS Lambda

Chaitanya picture Chaitanya · Sep 22, 2015 · Viewed 42.1k times · Source

I'm using AWS Lambda to scan data from a DynamoDB table. This is what I get in return:

{
  "videos": [
    {
      "file": {
        "S": "file1.mp4"
      },
      "id": {
        "S": "1"
      },
      "canvas": {
        "S": "This is Canvas1"
      }
    },
    {
      "file": {
        "S": "main.mp4"
      },
      "id": {
        "S": "0"
      },
      "canvas": {
        "S": "this is a canvas"
      }
    }
  ]
}

My front-end application is using Ember Data Rest Adapter which does not accepts such response. Is there any way I can get normal JSON format? There is this NPM module called dynamodb-marshaler to convert DynamoDB data to normal JSON. I'm looking for a native solution if possible.

Answer

David Rissato Cruz picture David Rissato Cruz · Dec 6, 2017

Actually you should use the unmarshall function from AWSJavaScriptSDK:

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

exports.handler = function( event, context, callback ) {
  const newImages = event.Records.map(
        (record) => AWS.DynamoDB.Converter.unmarshall(record.dynamodb.NewImage)
  );
  console.log('Converted records', newImages);
  callback(null, `Success`);
}