使用jq修改json中的键值 [英] Modify a key-value in a json using jq
问题描述
我有一个json,我想在其中修改特定的值,但是终端总是显示带有修改后的值的json,但实际上并没有更改特定文件中的值.样本json:
I have a json in which I want to modify a particular value but the terminal always displays the json with the modified value but it does not actually change the value in the particular file. Sample json:
{
name: 'abcd',
age: 30,
address: 'abc'
}
我想更改文件本身中的address的值,但到目前为止,我一直无法更改.我尝试使用:
I want to change the value of address in the file itself but so far I've been unable to do so. I tried using:
jq '.address = "abcde"' test.json
但是没有用.有什么建议吗?
but it didn't work. Any suggestions?
推荐答案
AFAIK jq
不支持就地编辑,因此您必须先重定向到一个临时文件,然后再用它替换原始文件,或使用<来自moreutils软件包的c1>实用工具,如下所示:
AFAIK jq
does not support in-place editing, so you must redirect to a temporary file first and then replace your original file with it, or use sponge
utility from the moreutils package, like that:
jq '.address = "abcde"' test.json|sponge test.json
还有其他重定向到相同文件"的技术,例如将输出保存在变量e.t.c中.如果您想了解更多有关"Unix&Linux StackExchange"的信息,那么这是一个不错的起点.
There are other techniques to "redirect to the same file", like saving your output in a variable e.t.c. "Unix & Linux StackExchange" is a good place to start, if you want to learn more about this.
这篇关于使用jq修改json中的键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!