Search code examples
jsonjqstring-interpolation

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

Solution

  • 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(","))"'