jq: slicing array based on argument

Viewed 5731

I am trying to slice an array in jq where the end index is passed as an argument from the shell (bash):

end_index=7
cat obj.json | jq --arg eidx $end_index, '.arr[0:$eidx]'

This works as expected when the index is hard-coded

cat obj.json | jq '.arr[0:7]'

but in the example at the top, I get an error message

jq: error (at <stdin>:0): Start and end indices of an array slice must be numbers

I suspect this might be to do with how jq handles variable substitution within the slice operator [:], but none of my attempts to solve the problem, e.g. by enclosing the variable name in curly braces .arr[0:${eidx}], has worked.

2 Answers
Related