Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq: Change multiple values

Tags:

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?

like image 421
OrangePot Avatar asked Nov 17 '17 17:11

OrangePot


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.

like image 148
peak Avatar answered Sep 30 '22 19:09

peak


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

like image 43
jq170727 Avatar answered Sep 30 '22 21:09

jq170727