如何重写 jq 上的确切值,引用顶部元素

How to rewrite the exact value on jq, referencing the top element

我有一个大的 json 看起来和这个很像

{
   "Report" : [
   {"blah" : "..."}
   ],
   "Actions" : [
      {
         "value" : "1",
         "properties" : {
            "name" : "abc",
            "age" : "2",
            "other": "test1"
          }
      },
      {
         "value" : "2",
         "properties" : {
            "name" : "def",
            "age" : "3",
            "other" : "test2"
          }
      }
   ]
}

我需要根据 "age" 的值更改 "other" 的值。

现在,我希望在终端上输出完整的 json,这样我就可以将它移动到 tmp 文件中。

此命令有效,但仅在终端上输出 Action 块

 jq '(.Actions[] | select (.properties.age == "3").properties.other = "no-test")'

此命令打印完整的 json,但会重写不应修改的键的值(注意 "no-test" 会针对 2 岁和 3 岁重写)。

jq '(. | select (.Actions[].properties.age == "3").Actions[].properties.other = "no-test")'

请告知是否有一种方法可以在块上进行更改,但在终端上输出完整的 json。

赋值打印执行赋值的整个对象,因此您可以将新值赋给修改后的 Actions 数组的 .Actions

.Actions=([.Actions[] | if .properties.age == "3" then .properties.other = "no-test" else . end])

我使用了 if 语句,但我们可以使用您的代码来做同样的事情

.Actions=[.Actions[] | select (.properties.age == "3").properties.other = "no-test"]

我使用 jqplay.org 创建了这些,这是开发人员提供的工具,使这些调试非常快速