0

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-"

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Would it create problems if the nested attribute did not begin with a number when I'm using an ExpressionAttributeName?
No. You can use safely use ExpressionAttributeNames for any or every attribute.

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.