尝试访问元素附近的无效路径表达式

Invalid path expression near attempt to access element

尝试更改数组中的单个元素时,我得到 Invalid path expression near attempt to access element - 但仅当从 --rawInput.

捕获数组时

示例:

# input: [ 1, 0 ]
. as $list | $list[0] = 30
# output: [ 30, 0 ]

但这不起作用:

# input: 1,0
split(",") | map(tonumber) as $list | $list[0] = 30
# Invalid path expression near attempt to access element 0 of [1,0]

有什么想法吗?

您的尝试失败,原因如下:

Note that the LHS of assignment operators refers to a value in .. Thus $var.foo = 1 won’t work as expected ($var.foo is not a valid or useful path expression in .); use $var | .foo = 1 instead.

来自Assignment section of the jq manual.

它可能只在您的第一个 jq 命令中起作用,因为 $list. 是相等的。

接下来您可以使用以下内容:

split(",") | map(tonumber) as $list | $list | .[0] = 30

或者更简单地说你的情况:

split(",") | map(tonumber) | .[0]=30