Mehran
Mehran

Reputation: 1304

DynamoDB update a field which has a JSON as value with integer/num-string as keys in python

I am storing an item with the following structure in my dynamodb table.

Item = {"response": [
    {
        "answers": {
            "11-18": 0,
            "19-24": 0
        }
    }
]}

I want to update response[0].answers.11-18 incrementally. My command was:

table_resource.update_item(
        Key={
            'id': 123
        },
        UpdateExpression="set response[0].answers.11-18 = response[0].answers.11-18 + :inc",
        ExpressionAttributeValues={":inc": 1},
        ReturnValues="UPDATED_NEW",
)

I get the following error.

An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "11", near: ".11-"

Upvotes: 0

Views: 1069

Answers (1)

Matthew Pope
Matthew Pope

Reputation: 7669

Your nested attribute 11-18 begins with a number, so you can’t use it in an update expression.

If an attribute name begins with a number or contains a space, a special character, or a reserved word, then you must use an expression attribute name to replace that attribute's name in the expression.

You can work around this by using placeholder ExpressionAttributeNames.

Upvotes: 2

Related Questions