Subhadip Banerjee
Subhadip Banerjee

Reputation: 47

jq update substring matched object

Json file:

{
  "A": "jack",
  "B": [
    {
      "name": "jack/jil",
      "version": "0.1"
    },
    {
      "name": "went/well",
      "version": "1.2"
    }
  ]
}

now I need to update every objects version: "$version-dev" where name starts from jack while retaining rest of the json intact.

the closest I can get.

jq '.B[] |  select(.name|test("jack.")) | .version += "-dev"' old.json > new.json

in the above command I'm only getting the that particular object with updated value in the new.json but I need the whole json too. any suggestions

Upvotes: 1

Views: 127

Answers (1)

pmf
pmf

Reputation: 36251

You need to put parantheses around the whole selection to be updated: (… | .version) += …

jq '(.B[] | select(.name|test("jack.")) | .version) += "-dev"' old.json > new.json
{
  "A": "jack",
  "B": [
    {
      "name": "jack/jil",
      "version": "0.1-dev"
    },
    {
      "name": "went/well",
      "version": "1.2"
    }
  ]
}

Demo

Upvotes: 3

Related Questions