Using jq to update objects within a JSON document if the value corresponding to a given key starts with a specified string

后端 未结 2 1267
旧时难觅i
旧时难觅i 2021-01-12 13:13

I have the given JSON and want to change the id value of all elements, which starts with test in the name element:

{
          


        
2条回答
  •  灰色年华
    2021-01-12 13:52

    You can also use map, like this:

    jq '(.values)|=(map((if .name|startswith("test") then .id="NEWID"  else . end)))' file
    

    Output:

    {
      "other-value": "some-id",
      "values": [
        {
          "name": "test-2017-12-01",
          "id": "NEWID"
        },
        {
          "name": "othert",
          "id": "2"
        }
      ]
    }
    

提交回复
热议问题