MrBubsters
MrBubsters

Reputation: 41

Update key values in dynamodb map where key has whitespaces

I am trying to update a key/value pair within a map object in my dynamodb table but am unable to use the key in an UpdateExpression because of whitespaces. The values I want to update are within this mapping several keys so in order to write the update expression. I am trying to use the syntax mainkey.subkey.targetkey to identify the key I want to update.

This causes issues however in the syntax if the target key has spaces. For example a key like "US 12" will cause an issue when implemented this way (mainkey.subkey.US 12).

Here is a snippet of code that can give context to the issue:

table_obj.update_item(
                Key=key,
                UpdateExpression='SET attributes.page2.extraction.US 12 = :newItem',
                ExpressionAttributeValues={
                    ":newItem": values
                },
                ReturnValues="UPDATED_NEW"
            )

Upvotes: 0

Views: 1090

Answers (1)

Nadav Har'El
Nadav Har'El

Reputation: 13731

This is what ExpressionAttributeNames is for. You can do something like this:

table_obj.update_item(
     Key=key,
     UpdateExpression='SET attributes.page2.extraction.#name = :newItem',
     ExpressionAttributeNames={'#name': 'US 12'},
     ExpressionAttributeValues={":newItem": values},
     ReturnValues="UPDATED_NEW"
)

Note that the alias #name is just for the single compoent "US 12" - you can't try to alias the entire path "attributes.page2.extraction.US 12".

Upvotes: 3

Related Questions