48

Given some DynamoDB JSON via a DynamoDB NewImage stream event, how do I unmarshall it to regular JSON?

{"updated_at":{"N":"146548182"},"uuid":{"S":"foo"},"status":{"S":"new"}} 

Normally I would use AWS.DynamoDB.DocumentClient, however I can't seem to find a generic Marshall/Unmarshall function.

Sidenote: Do I lose anything unmarshalling DynamoDB JSON to JSON and back again?

1

2 Answers 2

65

You can use the AWS.DynamoDB.Converter.unmarshall function. Calling the following will return { updated_at: 146548182, uuid: 'foo', status: 'new' }:

AWS.DynamoDB.Converter.unmarshall({ "updated_at":{"N":"146548182"}, "uuid":{"S":"foo"}, "status":{"S":"new"} }) 

Everything that can be modeled in DynamoDB's marshalled JSON format can be safely translated to and from JS objects.

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

7 Comments

ARGH.. what an awful API that it requires that M thing aws.DynamoDB.Converter.output({ 'M': record.dynamodb.NewImage })
AWS.DynamoDB.Converter.output is the part of the DocumentClient that translates DynamoDB AttributeValue objects to JSON-style objects. I assumed the M was part of the data you got in the stream event.
I opened a PR to improve the API by adding a marshall function that doesn't require the M key, and it was included in version 2.71.0 of the SDK.
Worked fine for me as of April 2020 using the AWS SDK Javascript API. Thanks a lot!
I know this is an old answer but this is not working for me and it seems related to the fact that a query can return a mix of json that can be decorated or not with datatyping. For exemple this doesn't work : { "Items": [ { "entityType": { "S": "member" } } ], "Count": 1, "ScannedCount": 1 }... will result in : { Items: undefined, Count: undefined, ScannedCount: undefined } .. even array [] alone doesn't work.
|
59

AWS SDK for JavaScript version 3 (V3) provides nice methods for marshalling and unmarshalling DynamoDB records reliably.

const { marshall, unmarshall } = require("@aws-sdk/util-dynamodb"); const dynamo_json = { "updated_at": { "N": "146548182" }, "uuid": { "S": "foo" }, "status": { "S": "new" } }; const to_regular_json = unmarshall(dynamo_json); const back_to_dynamo_json = marshall(to_regular_json); 

Output:

// dynamo_json { updated_at: { N: '146548182' }, uuid: { S: 'foo' }, status: { S: 'new' } } // to_regular_json { updated_at: 146548182, uuid: 'foo', status: 'new' } // back_to_dynamo_json { updated_at: { N: '146548182' }, uuid: { S: 'foo' }, status: { S: 'new' } } 

2 Comments

For this to work for me in TypeScript, I had to cast unmarshall(recordImage as { [key: string]: AttributeValue }); // AttributeValue = import { AttributeValue } from "@aws-sdk/client-dynamodb";
Wow! You have NO IDEA how hard this is to find.. I don't see this documented much if at all or I am blind in the DynamoDB docs and this seems like a must have tool when working with it!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.