编辑项目并使用jq打印整个内容

时间:2015-09-11 11:11:07

标签: json bash jq

说我有这样的JSON:

{
    "user" :
    {
        "name": "Manu",
        "songs": 30
    },
    "admin" :
    {
        "name": "Chao",
        "songs": 50
    }
}

我想将user -> songs替换为40并打印整个文件,以便我得到:

{
    "user" :
    {
        "name": "Manu",
        "songs": 40        <------ this is the line to change
    },
    "admin" :
    {
        "name": "Chao",
        "songs": 50
    }
}

如何使用jq(使用jq version 1.3)执行此操作?我尝试了下面的代码,它成功地替换了值,但是,并没有打印出整个内容:

$ jq "[.user | { "name": .name, "songs": 40 }]" myfile
[
  {
    "songs": 40,
    "name": "Manu"
  }
]

1 个答案:

答案 0 :(得分:3)

使用赋值运算符:

jq '.user.songs = 40' myfile