Description
Hi
I noticed something when i tried to use the current value for +=
. In the documentation it says:
jq has a few operators of the form a op= b, which are all equivalent to a |= . op b. So, += 1 can be used to increment values, being the same as |= . + 1.
But it seems like for a op= b
the input for the RHS is the input of the LHS and not . | a
.
$ jq -n '{a:1} | .a += (debug | 1)'
["DEBUG:",{"a":1}]
{
"a": 2
}
$ jq -n '{a:1} | .a |= . + (debug | 1)'
["DEBUG:",1]
{
"a": 2
}
In my case I was trying to do something similar to this. Add something to a list conditionally on the current list value:
$ jq -n '{a:[1,2,3]} | .a += [if length % 3 == 0 then 4 else empty end]'
{
"a": [
1,
2,
3
]
}
Here length
will get {a:[1,2,3]}
as input not [1,2,3]
and return 1
. But if i use |=
i get what i wanted:
$ jq -n '{a:[1,2,3]} | .a |= . + [if length % 3 == 0 then 4 else empty end]'
{
"a": [
1,
2,
3,
4
]
}
gojq seems to have the same behaviour which makes me think i might misunderstanding something? if so I can try to improve the documentation. If it's a bug it feels it might be a bit too late to change the behaviour?