Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq filter expression in string interpolation

I have been trying to reduce the array to a string to be used in string interpolation.

For example.

input = ["123", "456"]
expected output = array=123,456

Here is my try

$ echo '["123", "456"]' | jq 'array=\(.|join(","))'
jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at <top-level>, line 1:
array=\(.|join(","))
jq: 1 compile error
like image 581
Akshay Patil Avatar asked Jul 15 '26 03:07

Akshay Patil


2 Answers

Using string interpolation \(.), you can do something like below. Your idea is almost right, but interpolation using \(..) needs the filter to present be inside a string with the expression to be used defined inside parens after a backslash

jq --raw-output '"array=\(join(","))"'
like image 95
Inian Avatar answered Jul 17 '26 22:07

Inian


echo '["123", "456"]' | jq -r '"array=" + join(",")'
like image 42
William Pursell Avatar answered Jul 17 '26 23:07

William Pursell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!