jq: Change multiple values

Viewed 15080

I'm trying to change multiple json values with this line

 jq '.two="newval", .three="newval"' my.json 

with this is the input

{
  "one": {
    "val": 1
  },
  "two": "val",
  "three": "val",
  "four": "val"
}

but the output is 2 jsons:

{
  "one": {
    "val": 1
  },
  "two": "newval",
  "three": "val",
  "four": "val"
}
{
  "one": {
    "val": 1
  },
  "two": "val",
  "three": "newval",
  "four": "val"
}

How can I change multiple values and output in one item?

2 Answers

Simply change the comma to a pipe character and you’re done:

.two="newval" | .three="newval"

"," is for concatenating streams: A,B will emit A followed by B.

Here is a method which uses + object addition to update multiple members.

. + {two:"newtwo", three:"newthree"}

Sample Run (assumes data in data.json)

$ jq -M '. + {two:"newtwo", three:"newthree"}' data.json
{
  "one": {
    "val": 1
  },
  "two": "newtwo",
  "three": "newthree",
  "four": "val"
}

Try it online at jqplay.org

Related