Search code examples
amazon-dynamodbaws-step-functions

How can I use DynamoGetItem in a step function to get a normal object?


I'm trying to get an item from dynamo but it comes back as

{
    "Item": {
        "sk": {
            "S": "signup"
        },
        "pk": {
            "S": "user-123"
        },
        "email": {
            "S": "foo@bar.com"
        }
    },
    // header data, etc....

}

I'm trying

    // id-lookup table
    const table = Table.fromTableName(this, 'redacted', 'redacted');
    const getItem = new DynamoGetItem(this, 'GetItemStep', {
      table,
      key: {
        pk: DynamoAttributeValue.fromString(JsonPath.stringAt('$.body.req.id')),
        sk: DynamoAttributeValue.fromString('signup'),
      },
      resultSelector: {
        body: {
          'res.$': '$.Item',
        },
      },
      // outputPath: '$.Item',
    });

But I can't get rid of the obnoxious {S: any} thing its doing. I've done hours of research in JSONPath and I'm not convinced its at all possible to get an object, select a child, and maintain the keys.

I know about lambas, but I'm specifically looking for a stepFunction task or filter or similar.


Solution

  • StepFunction itself cannot retrieve the data in native JSON format as it uses the low level client and thats how it works. But when you retrieve the item, you can easily convert it with the unmarshall function:

    const { marshall, unmarshall } = require("@aws-sdk/util-dynamodb");
    
    let myItem = unmarshall(getFromSf())