Formatting DynamoDB data to normal JSON in AWS Lambda

前端 未结 8 1389
無奈伤痛
無奈伤痛 2020-12-01 02:55

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

{
  \"videos\": [
    {
      \"fil         


        
相关标签:
8条回答
  • 2020-12-01 03:10

    If you need online editor try this

    https://2json.net/dynamo

    0 讨论(0)
  • 2020-12-01 03:17

    I think it's just a custom transformation exercise for each app. A simple conversion from DynamoDB's item format to you application format might look like this:

    var response = {...} // your response from DynamoDB
    var formattedObjects = response.videos.map(function(video) {
        return {
            "file": video.file.S,
            "id": video.id.S,
            "canvas": video.canvas.S
        };
    });
    

    If you want to build a generic system for this, you would have to handle DynamoDB's various AttributeValue types. A function like the one below would do the job, but I've left out the hard work of handling most of DynamoDB's more complex attribute value types:

    function dynamoItemToPlainObj(dynamoItem) {
        var plainObj = {};
        for (var attributeName in dynamoItem) {
            var attribute = dynamoItem[attributeName];
            var attributeValue;
            for (var itemType in attribute) {
                switch (itemType) {
                case "S":
                    attributeValue = attribute.S.toString();
                    break;
                case "N":
                    attributeValue = Number(attribute.N);
                    break;
                    // more attribute types...
                default:
                    attributeValue = attribute[itemType].toString();
                    break;
                }
            }
            plainObj[attributeName] = attributeValue;
        }
        return plainObj;
    }    
    var formattedObjects = response.videos.map(dynamoItemToPlainObj);
    
    0 讨论(0)
  • 2020-12-01 03:21

    Here you can find gist which does that:

    function mapper(data) {
    
    let S = "S";
    let SS = "SS";
    let NN = "NN";
    let NS = "NS";
    let BS = "BS";
    let BB = "BB";
    let N = "N";
    let BOOL = "BOOL";
    let NULL = "NULL";
    let M = "M";
    let L = "L";
    
    if (isObject(data)) {
        let keys = Object.keys(data);
        while (keys.length) {
            let key = keys.shift();
            let types = data[key];
    
            if (isObject(types) && types.hasOwnProperty(S)) {
                data[key] = types[S];
            } else if (isObject(types) && types.hasOwnProperty(N)) {
                data[key] = parseFloat(types[N]);
            } else if (isObject(types) && types.hasOwnProperty(BOOL)) {
                data[key] = types[BOOL];
            } else if (isObject(types) && types.hasOwnProperty(NULL)) {
                data[key] = null;
            } else if (isObject(types) && types.hasOwnProperty(M)) {
                data[key] = mapper(types[M]);
            } else if (isObject(types) && types.hasOwnProperty(L)) {
                data[key] = mapper(types[L]);
            } else if (isObject(types) && types.hasOwnProperty(SS)) {
                data[key] = types[SS];
            } else if (isObject(types) && types.hasOwnProperty(NN)) {
                data[key] = types[NN];
            } else if (isObject(types) && types.hasOwnProperty(BB)) {
                data[key] = types[BB];
            } else if (isObject(types) && types.hasOwnProperty(NS)) {
                data[key] = types[NS];
            } else if (isObject(types) && types.hasOwnProperty(BS)) {
                data[key] = types[BS];
            }
        }
    }
    
    
    return data;
    
    function isObject(value) {
        return typeof value === "object" && value !== null;
    }
    

    }

    https://gist.github.com/igorzg/c80c0de4ad5c4028cb26cfec415cc600

    0 讨论(0)
  • 2020-12-01 03:23

    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`);
    }
    
    0 讨论(0)
  • 2020-12-01 03:26

    AWS JavaScript SDK was recently updated with Document Client which does exactly what you need. Check the announce and usage examples here: http://blogs.aws.amazon.com/javascript/post/Tx1OVH5LUZAFC6T/Announcing-the-Amazon-DynamoDB-Document-Client-in-the-AWS-SDK-for-JavaScript

    0 讨论(0)
  • 2020-12-01 03:34

    I know is a bit old but I had the same problem processing stream data from dynamoDB in node js lambda function. I used the proposed by @churro

    import sdk and output converter

    var AWS = require("aws-sdk");
    var parse = AWS.DynamoDB.Converter.output;
    

    use the parse function with a small hack

    exports.handler = function( event, context, callback ) {
      var docClient = new AWS.DynamoDB.DocumentClient();
      event.Records.forEach((record) => {
            console.log(record.eventID);
            console.log(record.eventName);
            console.log('DynamoDB Record:', parse({ "M": record.dynamodb.NewImage }));
        });
      callback(null, `Successfully processed ${event.Records.length} records.`);
    }
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题