#3022·jq

Setting non-existent elements of array slices uncovers hidden elements

Author: nicowilliamsCreated Jan 26, 2024Updated Aug 10, 2026
Labelsbuglibjq

Extending a slice by setting an element that is "hidden" by the slice reveals elements between the last one of the slice and the element being set:

bash
: ; jq -cn '[range(10)][0:3]|(.,(.[5] = true))'
[0,1,2]
[0,1,2,3,4,true]

Appending to a slice works:

bash
: ; jq -cn '[range(10)][0:3]|(.,(.+[true]))'
[0,1,2]
[0,1,2,true]

But then:

bash
: ; jq -cn '[range(10)][1:3]|[.,(.[5] = true)]'
[[1,2],[1,2,null,null,null,true]]

Using offsets other than zero in the slice does not make things worse, fortunately:

bash
: ; jq -cn '[range(10)][1:3]|(.,(.[5] = true))'
[1,2]
[1,2,3,4,5,true]
: ; jq -cn '[range(10)][1:3]|(.,(.+[true]))'
[1,2]
[1,2,true]