1

I am retrieving data from DynamoDB using a query and I get the following returned:

[{"serviceUserId":{"S":"123456789"},"createdDate":{"S":"11-12-2021"}}]

The DynamoDB JSON format has the type in in which I am trying to get rid of by converting to a normal JSON format. I have tried using the AWS.DynamoDB.Converter.unmarshall but I am getting an error in my code:

Argument of type 'ItemList' is not assignable to parameter of type "AttributeMap".
  Index signature for type 'string' is missing in type "AttributeMap[]".

Here is my code:

                 if (result.Count > 0) {
                     const newImage = AWS.DynamoDB.Converter.unmarshall(
                        result.Items
                         )
                   console.log('new Image: ' + JSON.stringify(newImage));
                    resolve(newImage);
                 } else { 
                     console.log('No record found');
                     reject(err);
                 }

If I remove the [] brackets in the DynamoDB JSON then it is converted successfully, but obviously I cannot do this in my program as the brackets are there for a reason!

Does anyone know how to convert my JSON file to a format that unmarshall will accept?

2
  • What is the value of result? What is result.Items? Commented Dec 17, 2021 at 17:11
  • result and result.items is '[object Object]' and when I stringify it I get '[{"serviceUserId":{"S":"123456789"},"createdDate":{"S":"11-12-2021"}}]' Commented Dec 17, 2021 at 17:21

1 Answer 1

9

Map through the items and unmarshal one by one. (un)marshal accepts an object type, not an array.

import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';  // SDK V3, but worked the same in V2

(() => {
  const items = [{ serviceUserId: { S: '123456789' }, createdDate: { S: '11-12-2021' } }];
  
  // from DynamoDB JSON
  const unmarshalled = items.map((i) => unmarshall(i));

  // make the return trip back to DynamoDB JSON
  const marshalled = unmarshalled.map((i) => marshall(i));
})();
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks - I shall give it a test a bit later but it all seems to make sense so hopefully it will work!
Unfortunately, this didn't quite work as my items variable has strings: '[{"serviceUserId":{"S":"123456789"},"createdDate":{"S":"11-12-2021"}}]' and yours was an object: 'const items = [{ serviceUserId: { S: '123456789' }, createdDate: { S: '11-12-2021' } }];' Unmarshall does not accept strings.
In addition, the DynamoDB data could have multiple items returned: '[{"serviceUserId":{"S":"123456789"},"createdDate":{"S":"11-12-2021"}},{"serviceUserId":{"S":"123456790"},"createdDate":{"S":"11-12-2021"},{"serviceUserId":{"S":"123456791"},"createdDate":{"S":"11-12-2021"}}]'
Any ideas to solve this?
Convert your string into JSON with JSON.parse. Parsing will fail, however, if the string is not proper JSON. The multiple items string in your comment won't parse because the second element is missing a closing bracket. The answer will handle an array of items. That's what map does.
|

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.