- Newest
- Most votes
- Most comments
To handle AWSJSON in an AppSync JavaScript resolver, you're on the right track, but there are a few adjustments you can make to ensure the metadata is correctly stored in your DynamoDB table.
First, it's important to understand that AWSJSON is indeed received as a stringified JSON in the resolver. Your approach of using JSON.parse() is correct, but the issue might be in how you're applying the update operation.
Here's an improved version of your resolver code:
export function request(ctx) { const { guid, metadata, ...rest } = ctx.args.assetDetail; const updateExpression = {}; Object.keys(rest).forEach(key => { updateExpression[key] = { Action: 'PUT', Value: rest[key] }; }); // Parse the metadata JSON string const parsedMetadata = JSON.parse(metadata); updateExpression.metadata = { Action: 'PUT', Value: parsedMetadata }; return { operation: 'UpdateItem', key: { guid: { S: guid } }, update: { expression: 'SET #metadata = :metadata, #rest = :rest', expressionNames: { '#metadata': 'metadata', '#rest': 'rest' }, expressionValues: { ':metadata': { M: parsedMetadata }, ':rest': { M: updateExpression } } } }; }
In this updated version:
- We parse the metadata JSON string using JSON.parse().
- We create an updateExpression object to handle all other fields.
- We use the 'UpdateItem' operation directly, which gives us more control over how the update is performed.
- We set the metadata as a map (M) in DynamoDB, which is the correct way to store JSON objects.
- We use expression attributes to avoid potential naming conflicts.
This approach should correctly store your metadata as a JSON object in the DynamoDB table, rather than as null.
Remember to adjust the key type (S for string) in the 'key' section if your guid is not a string.
Also, ensure that your IAM permissions for the resolver are set up correctly to allow the UpdateItem operation on your DynamoDB table.
If you're still experiencing issues, double-check that the incoming metadata is valid JSON and that there are no type mismatches between what's being sent and what DynamoDB expects.
Lastly, consider using AWS AppSync's built-in $util.toJson() and $util.parseJson() functions if you need to perform JSON operations in your resolver, as these are optimized for use within AppSync.
Sources
Configuring resolvers in AWS AppSync - AWS AppSync
Testing and debugging resolvers in AWS AppSync (JavaScript) - AWS AppSync
AWS AppSync JavaScript resolvers overview - AWS AppSync
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated 4 years ago

Ah ok, so that was my suspicion, that I would end up having to write out my updateExpress with names and values. So there's no way to utilize the much easier
@aws-appsync/utilsstuff?And how was I supposed to troubleshoot this on my own? As you can see in the test output from my resolver, the update expression LOOKS correct,
metadatais a Map/Mtype.To your last point, I can't use
$util.toJsonsince I believe that only exists in VTL resolvers.Yeah this still isn't working. With this code verbatim I get the error
Value for attribute '$[update][expressionValues][:metadata][M]' must be an object.And I'm not sure how this code would work anyway. There is no ddb prop called
rest, I need that flattened out and as a sibling ofmetadata. :(I really can't see why this doesn't work. I reduced my resolver code to a very simple:
Running my resolver in AppSync shows this return from my request resolver, which according to the docs, looks like it should work:
However,
metadatais still getting set tonull.I'm probably just going to rework this as a lambda. I've wasted too much time on this.