Search code examples
javascriptamazon-dynamodbunmarshalling

Unmarshall of string set in DynamoDB fails


I have tried to marshall DynamoDB's String Set and unmarshall it back as below.

import {marshall, unmarshall} from '@aws-sdk/util-dynamodb';

test('Marshall and Unmarshall Test', () => {
  const raw = {
    'anArray': new Set([
      'Apple',
      'Mango'
    ])
  };

  console.log(JSON.stringify(marshall(raw)));
  // {"anArray":{"SS":["Apple","Mango"]}}

  const marshalledResult = marshall(raw);

  console.log(JSON.stringify(unmarshall(marshalledResult)));
  // {"anArray":{}}

});

But I am not able to get the String Set back. How should we be doing this when we should get a String Set from dynamo db and use it?


Solution

  • unmarshall is the right approach. I believe you are not seeing the expected result because JSON.stringify does not play nicely with sets. Try logging the unmarshalled result directly:

    const unmarshalled = unmarshall(marshalledResult)
    console.log(unmarshalled);
    // => { anArray: Set(2) { 'Apple', 'Mango' } }