Marco
Marco

Reputation: 726

From DynamodbStreamRecord map to JSON string

Let's say I have this situation in which I got a DynamodbStreamRecord inside an AWS Lambda. From this stream record (variable named record), I have a chain of Java methods that extracts a map in this way:

Map<String, AttributeValue> w1Data = record.getDynamodb().
    getNewImage().  // obtain the image
    get("DT").      // get from key "DT"
    getM().         // obtain the related map
    get("w1_data"). // get from key "w1_data"
    getM();         // obtain the related map

Now, I need to transform such w1Data map in a JSON string and I tried to use the org.json.JSONObject constructor that takes a map as input parameter, followed by a toString():

String jsonRepr = new JSONObject(w1Data).toString();

But I obtained this strange string:

'{"SessionExtraInfo":"{M: {Info={M: {CampaignID={N: 3,}, OriginID={N: 1,}, EntitySourceClassID={N: 8,}},}},}"}'

which instead should be something like this:

'{"SessionExtraInfo": {"Info": {"OriginID": "1", "CampaignID": "3", "EntitySourceClassID": "8"}}}'

Do you have any suggestion to create a valid JSON string from this map without showing the data types specified by DynamoDB?

Thank you very much

Upvotes: 3

Views: 3623

Answers (1)

Samhash
Samhash

Reputation: 140

DynamoDB Stream has its own JSNON format in which it has an extra key with every value which describes its TYPE i.e S for String, N for Number and BOOL for Boolean value.

You should look into this link http://blogs.aws.amazon.com/javascript/post/Tx1OVH5LUZAFC6T/Announcing-the-Amazon-DynamoDB-Document-Client-in-the-AWS-SDK-for-JavaScript

This has solved another SO problem that resembles yours.

Upvotes: 1

Related Questions