Ethan
Ethan

Reputation: 3798

How to convert DynamoDB JSON to a regular Javascript object

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"
}

Upvotes: 2

Views: 4515

Answers (2)

phyatt
phyatt

Reputation: 19102

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)

Upvotes: 0

Ethan
Ethan

Reputation: 3798

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

Upvotes: 15

Related Questions