使用 jq 编辑子 JSON 元素值

Mab*_*boo 10 command-line replace json jq

我正在尝试使用 jq 来改变孩子的价值。例如,我想将“test2”的值从 ["yo", "bye"] 更改为 ["hi"]。

{
   "title": "hello",
   "body": {
       "test1": 123,
       "test2": [
           "yo",
           "bye"  
       ]
   }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我只能更改作为根子项的键的值。我不知道如何使用它并转到 .body.test2 而不是标题:

cat test.conf | jq 'to_entries | map(if .key == "title" then . + {"value":"hello world"} else . end) | from_entries' > test2.conf
Run Code Online (Sandbox Code Playgroud)

Del*_*gan 20

(发布@glennjackman评论作为社区答案以防止系统自动删除问题)

jq '.body.test2 = ["hi"]' 会做的