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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 Did you figure out any solution for this problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.