Home > database >  How can we have a ProjectionExpression on a key that is an integer
How can we have a ProjectionExpression on a key that is an integer

Time:09-08

I have a scenario where The data is structured such as this.

{
  "PartitionKey": "foobar",
  "SomeDict": {
    "10": "a value",
    "20": "another value",
    ...
  }
}

I want to have a projection expression to only read one of the values. The "naive" way would be to do the following query:

get_item(
  Key={"PartitionKey": "foobar"},
  ProjectionExpression="SomeDict.10"
)

But it fails with the following error: An error occurred (ValidationException) when calling the GetItem operation: Invalid ProjectionExpression: Syntax error; token: "10", near: ".10"

Is there a way to have projection expressions on keys that are integers, or is that a limitation?

Thanks!

CodePudding user response:

From the docs:

You can use any attribute name in a projection expression, provided that the first character is a-z or A-Z and the second character (if present) is a-z, A-Z, or 0-9. If an attribute name does not meet this requirement, you must define an expression attribute name as a placeholder.

You need to provide ExpressionAttributeNames. To add to your example it would be something like:

get_item(
  Key={"PartitionKey": "foobar"},
  ProjectionExpression="#somedict.#ten",
  ExpressionAttributeNames={"#somedict":"SomeDict", "#ten":"10"}
)

Read more here from the Amazon DynamoDB docs

  • Related