Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid path expression near attempt to access element

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]
  • Working example
  • Not working example

Any ideas?

like image 412
Remy Sharp Avatar asked Dec 13 '19 14:12

Remy Sharp


1 Answers

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
like image 119
Aaron Avatar answered Oct 13 '22 01:10

Aaron