How to get the pure Json string from DynamoDB stream new image?

為{幸葍}努か 提交于 2019-11-29 03:58:53

Below is the complete code for converting from Dynamo JSON to Standard JSON:

import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.internal.InternalUtils;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.amazonaws.services.lambda.runtime.events.DynamodbEvent.DynamodbStreamRecord;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Main Lambda class to receive event stream, parse it to Survey
 * and process them.
 */
public class SurveyEventProcessor implements
        RequestHandler<DynamodbEvent, String> {

    private static final String INSERT = "INSERT";

    private static final String MODIFY = "MODIFY";

    public String handleRequest(DynamodbEvent ddbEvent, Context context) {

        List<Item> listOfItem = new ArrayList<>();
        List<Map<String, AttributeValue>> listOfMaps = null;
        for (DynamodbStreamRecord record : ddbEvent.getRecords()) {

            if (INSERT.equals(record.getEventName()) || MODIFY.equals(record.getEventName())) {
                listOfMaps = new ArrayList<Map<String, AttributeValue>>();
                listOfMaps.add(record.getDynamodb().getNewImage());
                listOfItem = InternalUtils.toItemList(listOfMaps);
            }

            System.out.println(listOfItem);
            try {
               // String json = new ObjectMapper().writeValueAsString(listOfItem.get(0));
                Gson gson = new Gson();
                Item item = listOfItem.get(0);

                String json = gson.toJson(item.asMap());
                System.out.println("JSON is ");
                System.out.println(json);
            }catch (Exception e){
                e.printStackTrace();
            }
        }


        return "Successfully processed " + ddbEvent.getRecords().size() + " records.";
    }
} 

In c# you can convert newImage to pure json by use of DynamoDB Document class

using Amazon.DynamoDBv2.DocumentModel;

var streamRecord = dynamoEvent.Records.First();

var jsonResult=Document.FromAttributeMap(streamRecord.Dynamodb.NewImage).ToJson();


and if you want to go further ahead to convert json to object you can use Newtonsoft

using Newtonsoft.Json;

TModel model = JsonConvert.DeserializeObject(jsonResult);

Found a way of doing it cleanly. Using InternalUtils from aws-java-sdk-dynamodb-1.11.15.jar

com.amazonaws.services.dynamodbv2.model.Record streamRecord = ((RecordAdapter) record).getInternalObject();
            // get order ready //
            OrderFinal order = Utils.mapO2Object(
                    InternalUtils.toSimpleMapValue(streamRecord.getDynamodb().getNewImage().get("document").getM()), 
                    OrderFinal.class );
lost in binary

Just summarizing the answer of Himanshu Parmar:

Map<String, AttributeValue> newImage = record.getDynamodb().getNewImage();
List<Map<String, AttributeValue>> listOfMaps = new ArrayList<Map<String, AttributeValue>>();
listOfMaps.add(newImage);
List<Item> itemList = ItemUtils.toItemList(listOfMaps);
for (Item item : itemList) {
    String json = item.toJSON();
}
shailender arya

Did you figure out a way to do this. One crude way is to create your own parser but even we don't want to go with that approach

This library do the job: dynamoDb-marshaler

var unmarshalJson = require('dynamodb-marshaler').unmarshalJson;

console.log('jsonItem Record: %j', unmarshalJson(record.dynamodb.NewImage));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!