我有一个简单的 JQ 过滤器,可以根据“Key”更新数组中的某些值。例如,这是我的输入:
[
{
"Key": "IDontCare",
"Value": "something"
},
{
"Key": "Tag1",
"Value": "123-456"
},
{
"Key": "Tag2",
"Value": "121-717"
}
]
我想要将这两个标签的值更新为一个新值(相同的值),所以我有了这个命令,它能够操作成功:
jq --arg NEW_VALUE '987-654' \
'[.[] |= if(.Key=="Tag1" or .Key=="Tag2") then (.Value=$NEW_VALUE) else . end]'
我希望在不同的运行中更新不同的标签,并将它们作为另一个参数传递。但是不确定如何更改if()
以从参数中查找标签。
我尝试了类似这样的东西,但显然不是正确的方式:
jq --argjson TAGS '["Tag1","Tag2"]' --arg NEW_VALUE '987-654' \
'[.[] |= if(.Key|in($TAGS)) then (.Value=$NEW_VALUE) else . end]'
有什么想法吗?
jq: error: IN/1 is not defined at <top-level>
。 - MLuselect( .Key | any($TAGS[] == .; .) )
,但在1.5中仍然无法工作。不过,在1.6中仍然有效。 - MLu