6

When trying to change a single element in an array, I get Invalid path expression near attempt to access element - but only when the array is captured from --rawInput.

Example:

# input: [ 1, 0 ]
. as $list | $list[0] = 30
# output: [ 30, 0 ]

But this doesn't work:

# input: 1,0
split(",") | map(tonumber) as $list | $list[0] = 30
# Invalid path expression near attempt to access element 0 of [1,0]

Any ideas?

peak
  • 105,803
  • 17
  • 152
  • 177
Remy Sharp
  • 4,520
  • 3
  • 23
  • 40

1 Answers1

7

Your attempt failed because of following :

Note that the LHS of assignment operators refers to a value in .. Thus $var.foo = 1 won’t work as expected ($var.foo is not a valid or useful path expression in .); use $var | .foo = 1 instead.

From the Assignment section of the jq manual.

It likely only worked in your first jq command because $list and . were equal.

Following that you could have used the following :

split(",") | map(tonumber) as $list | $list | .[0] = 30

Or more simply in your case :

split(",") | map(tonumber) | .[0]=30
Aaron
  • 24,009
  • 2
  • 33
  • 57