如何重写JQ上的精确值,引用顶级元素 [英] How to rewrite the exact value on jq, referencing the top element

查看:131
本文介绍了如何重写JQ上的精确值,引用顶级元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的JSON,类似于此

I have a large json that looks similar to this

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

和我需要改变的其他视年龄的值的值。

and I need to change the value of "other" depending on the value of "age".

现在,我要完整的JSON是终端显示出来,这样我就可以将它移到tmp文件。

Now, I want the complete json to be output on the terminal, so I can move it to tmp file.

此命令的工作,但只输出的动作块的终端上

This command works but outputs only the Action block on the terminal

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

此命令打印完整的JSON,但重写值不应被修改的键。

This command prints the complete json, but rewrites the value for keys that should not be modified (notice "no-test" gets re-written for both ages 2 and 3).

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

请告知,如果有到块的改变防区的方式,但输出终端上的完整的JSON。

Please advise if there is a way to zone in on the block to the changed, yet output the complete json on the terminal.

推荐答案

分配打印与执行的,所以你可以指定一个新值 .Actions 转让的整个对象修改后的操作阵列

Assignment prints the whole object with the assignment executed so you could assign a new value to .Actions of the modified Actions array

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

我用了一个if语句,但是我们可以使用code做同样的事情。

I used an if statement but we can use your code to do the same thing

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

我创建这些使用 jqplay.org 这是一个工具的开发竖起,使这些真正快速调试

I created these using jqplay.org which is a tool the dev put up, makes these really quick to debug

这篇关于如何重写JQ上的精确值,引用顶级元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆