且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解析参数“--expression-attribute-values"时出错:JSON 无效:需要用双引号括起来的属性名称:第 1 行第 3 列(字符 2)

更新时间:2023-10-24 09:22:04

有两个迫在眉睫的问题:

There are two immediate problems:

  • '(单引号)在 JSON 中不是有效的字符串分隔符;您必须使用"(双引号):

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

遗憾的是,从 v7.1 开始 PowerShell 要求您在调用外部程序时 -escape argument-internal " 个字符即使应该没有必要.

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

因此,试试这个;请注意 '...' 如何用于外部引用(PowerShell 在幕后将其转换为双引号),以便您需要't 在字符串内将 " 转义为 `" - 请注意,字符串内容随后被逐字处理;
但是,从 PowerShell 7.1 开始,在调用诸如 aws 之类的外部程序时, 转义总是需要的:

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


如果您确实需要 "..." 作为外部引用 以使用字符串扩展(插值),即,为了嵌入变量引用和表达式,事情变得更丑了,因为你需要应用两种转义:`"首先,以满足PowerShell的语法要求,在通过 确保生成的嵌入 " 正确传递到目标程序:


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 可以减轻痛苦,但请注意它总是使命令 多行 - 并且 -escaping 的需要仍然适用(注意"@,结束定界符不仅必须在它自己的行上,而且必须在该行的开头):


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