Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
}
like image 467
Ethan Avatar asked Apr 27 '26 03:04

Ethan


2 Answers

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
like image 153
Ethan Avatar answered Apr 30 '26 05:04

Ethan


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)
like image 34
phyatt Avatar answered Apr 30 '26 05:04

phyatt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!