2

How can I convert a DynamoDB JSON object to a regular object in JavaScript?

Example DynamoDB object:

{
 "key1": {
  "S": "val1"
 },
 "key2": {
  "S": "val2"
 },
 "key3": {
  "M": {
   "key4": {
    "M": {
     "key5": {
      "S": "val5"
     }
    }
   }
  }
 },
 "key6": {
  "S": "val6"
 }
}

Expected output:

{
 "key1": "val1",
 "key2": "val2",
 "key3": {
  "key4": {
   "key5": "val5"
  }
 },
 "key6": "val6"
}

2 Answers 2

15

You can use the unmarshall function in the @aws-sdk/util-dynamodb library.

const { unmarshall } = require("@aws-sdk/util-dynamodb");

const regularObject = unmarshall(dynamoObject);

console.log(regularObject); // Will output converted object
Sign up to request clarification or add additional context in comments.

Comments

1

I found a similar solution here:

https://github.com/dangerfarms/unmarshall-dynamodb-json

https://dangerfarms.github.io/unmarshall-dynamodb-json/

The unmarshalling line looks like this:

AWS.DynamoDB.Converter.unmarshall(dynamodbJson)

1 Comment

A simple straigntforward browser js example. Thank you !

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.