Specify multiple values

Given this file

{
  "[global]": {
    "current": "",
    "hash": ""
  }
}

      

I need this output:

{
  "[global]": {
    "current": "alpha",
    "hash": "bravo"
  }
}

      

I have this working command:

jq '."[global]".current="alpha" | ."[global]".hash="bravo"' example.json

      

However, I would rather not repeat the part ."[global]"

. I tried this, but only returns part of the input:

$ jq '."[global]" | .current="alpha" | .hash="bravo"' example.json
{
  "current": "alpha",
  "hash": "bravo"
}

      

+3


source to share


2 answers


multiplication of objects recursively combines them. You can merge object [global]

with object with new values. The string values ​​in RHS will be used in the result.

."[global]" *= { current: "alpha", hash: "bravo" }

      



Addtion will work here as well, but multiplication is generally more useful, especially with nested objects. Instead of replacing the corresponding objects, they are also merged.

+3


source


As pointed out by Jeff , the non-recursive operator will work in this situation as well +=

. In particular:



  ."[global]" += {current: "alpha", hash:"bravo"}

      

0


source







All Articles