12

I am running the following query

aws dynamodb query `
--table-name user`
--key-condition-expression "datecreated = :d" `
--expression-attribute-values "{ ':d': { 'S': '2018-08-15' } }" --endpoint-url http://localhost:8000

Does dynamodb even understand what a double quote is?

  1. I have tried swapping single quotes with double quotes.
  2. Using double quotes everywhere
  3. Doubling up the quotes, both single and double
  4. Using slashes
  5. Removing single quotes altogether
3
  • Not familiar with AWS Dynamodb, but how are you utilizing PowerShell in this? Commented Aug 15, 2018 at 15:47
  • 1
    @trebleCode: He's calling the aws utility from PowerShell (a script, presumably), as suggested by the tagging and also by the use of ` as the line-continuation character. Commented Aug 15, 2018 at 16:00
  • @trebleCode through the AWS CLI Commented Nov 26, 2020 at 13:44

4 Answers 4

16

There are two immediate problems:

  • ' (single quotes) aren't valid string delimiters in JSON; you must use " (double quotes):

  • Sadly, as of v7.2 PowerShell requires you to \-escape argument-internal " characters when calling external programs, even though that shouldn't be necessary.

    • See this answer for details, including a potential future (possibly opt-in) fix.
    • As an alternative to the manual \-escaping detailed below, you can use the PSv3+ ie helper function from the Native module (in PSv5+, install with Install-Module Native from the PowerShell Gallery), which internally compensates for all broken behavior and allows passing arguments as expected; to use it, simply prepend ie to your invocations; e.g.:
      ie aws dynamodb query ...

Therefore, try this; note how '...' is used for the outer quoting (which PowerShell transforms to double quotes behind the scenes) so that you needn't escape " as `" inside the string - do note that the string content is then treated literally;
The \-escaping, however, is always needed when calling an external program such as aws as of PowerShell 7.2:

... --expression-attribute-values '{ \":d\": { \"S\": \"2018-08-15\" } }'

If you do need "..." as the outer quoting in order to use string expansion (interpolation), i.e., in order to embed variable references and expressions, things get uglier, because you need to apply two kinds of escaping: `" first, to satisfy PowerShell's syntax requirements, preceded by \ to ensure the resulting embedded " are correctly passed through to the target program:

$date = [datetime]::now.ToString('yyyy-MM-dd')
... --expression-attribute-values "{ \`":d\`": { \`"S\`": \`"$date\`" } }"

A here-string can ease the pain, but note that it invariably makes the command multi-line - and the need for \-escaping still applies (note that "@, the closing delimiter must not only be on its own line, it must be at the very start of that line):

... --expression-attribute-values @"
  { \":d\": { \"S\": \"$date\" } }
"@
Sign up to request clarification or add additional context in comments.

1 Comment

I prefer the alternative option of using bash/ubuntu as the associated apps work better with docker
4

I've gone through the same issue just now when using Key-Condition-Expression Parameter

aws dynamodb query --table-name mystore --key-condition-expression "clientid = :name" --expression-attribute-values '{":name":{"S":"[email protected]"}}'

which would give

enter image description here

In order to solve it, just had to change the command to (note the \")

aws dynamodb query --table-name mystore --key-condition-expression "clientid = :name" --expression-attribute-values '{\":name\":{\"S\":\"[email protected]\"}}'

enter image description here

Comments

2

It's frustrating that this doesn't work more simply.

I have always double converted the object to json, like this:

$cliValue = $myObjectGraph | ConvertTo-Json -Depth 9 -Compress | ConvertTo-Json -Depth 9;

Which gives me $cliValue in this form:

"{\"ids\":[[\"94143814-c212-41b1-983f-e2d8ff589b2a\",\"My Description 1\"],[\"418a5f04-3d5a-4516-961f-6b0ab448edbb\",\"My Description 2\"], ...

Comments

1

I encountered this problem when I wanted to create a bash script that accept an argument so that I can make a query with various input.

One way to avoid the problem was to use temporary json file.

In bash script:

attrvalues="{\":d\": {\"S\": \"$1\"}}"
echo "$attrvalues" > attr-values.json

which would create a file below if you provide 2021-01-31 as first input:

{
  ":d": {"S": "2021-01-31"}
}

Then in bash script:

aws dynamodb query \
    --table-name user \
    --key-condition-expression "datecreated = :d" \
    --expression-attribute-values file://attr-values.json \
    --endpoint-url http://localhost:8000

rm attr-values.json

Comments

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.