Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

amazon web services - Error parsing parameter '--expression-attribute-values': Invalid JSON: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two immediate problems:

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

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

    • See this documentation issue for details and this longstanding bug report.
    • 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.1:

... --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" } }
"@

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...