在 bash 中操作 JSON

Bas*_*974 6 bash json

我有一个 JSON 文件,我需要更新一个特定的值。

{
  "Comment": "comment",
  "test": {
    "enabled": true
  },
  "enabled": true,
  "otherStuff": blah,
  "otherStuff2": blah,
  "otherStuff3": blah,
}
Run Code Online (Sandbox Code Playgroud)

我想将第二个“启用”的值更改为 false。使用 JQ Parser,我可以使用jq '.enabled '轻松检索它,但我不确定操作 JSON 的最佳方法是什么。

JSON 是我从 API 获得的响应,将来可能会发生变化,我不能依赖之前/之后的行或值。

gle*_*man 14

快速实验:

$ echo '{
  "Comment": "comment",
  "test": {
    "enabled": true
   },
  "enabled": true,
  "otherStuff": "blah",
  "otherStuff2": "blah",
  "otherStuff3": "blah"
}' |
jq '.enabled=false'
Run Code Online (Sandbox Code Playgroud)
{
  "otherStuff3": "blah",
  "otherStuff2": "blah",
  "otherStuff": "blah",
  "enabled": false,
  "test": {
    "enabled": true
  },
  "Comment": "comment"
}
Run Code Online (Sandbox Code Playgroud)