3

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

Marco
  • 700
  • 1
  • 14
  • 26

1 Answers1

1

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.

Community
  • 1
  • 1
Samhash
  • 140
  • 3
  • 15
  • 1
    Thank you @Samhash. Yes, I already know about the dedicated JSON representation for DynamoDB and indeed, as I wrote, I'm looking for a possible solution without showing the data types specified by DynamoDB. Anyway, I'm using Java, not Javascript, so the DocumentClient from Javascript AWS SDK is not useful. A possible solution could be to implement a custom method to perform the "translation" but maybe there is a "magic method" from Java SDK I can use but I can't figure out which one. – Marco Aug 05 '16 at 08:05
  • @Marco Did you figure out any solution for this problem? – Ram Mittala Jul 17 '17 at 20:48
  • duplicated: https://stackoverflow.com/questions/37655755/how-to-get-the-pure-json-string-from-dynamodb-stream-new-image – devwebcl Feb 19 '20 at 20:49