zstadler
zstadler

Reputation: 379

puzzling behavior of `.[]|=empty`

As far as I understand, the use of empty in update-assignment |= deletes the entry, similar to del(path), and del(path) also works in arrays.

I tried to selectively delete and modify array entries, and got strange results. While trying to understand the problem, I minimized the code to this:

Filter: .[]|=empty
Input: [0,1,2,3,4,5]
Output:
[ 1, 3, 5 ]

Try it online here

Apparently, only even array indexes are deleted. Why?

Upvotes: 3

Views: 70

Answers (1)

peak
peak

Reputation: 116957

As noted in a comment, jq's treatment of .[] |= empty has varied over time. One might expect that for every array, A, A | (.[] |= empty) would yield [] on the theory that the expression should result in every item in A being replaced by empty.

In any case, the current (jq 1.6) implementation is clearly wrong, as is particularly noticeable in this example:

jq-1.6 -n  '[0,1,2,3] | (.[] |= if . == 2 then empty else . end)'
[
  0,
  1,
  3,
  null
]

Bug reports can be submitted to https://github.com/stedolan/jq/issues

Upvotes: 1

Related Questions