You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This assumes, however, that the elements of the array are themselves valid JSON values. A counterexample is x=(1 $'a\nb' 2), where the second element contains a literal newline.
I believe the only safe way to do this is with multiple calls to jq, building the array one element at a time:
arr='[]'
for element in "${x[@]}"; do
arr=$(jq -n --arg elt "$element" --argjson "$arr" '$arr + [$x]')
done
to ensure that each element of x is properly encoded before adding it to the array.
The text was updated successfully, but these errors were encountered:
$ x=(1 $'a\nb' 2)
$ jq -n --argjson args "$(printf '%s\0' "${x[@]}" | jq -Rc 'split("\u0000")')" '$args'
jq: invalid JSON text passed to --argjson
Use jq --help for help with command-line options,
or see the jq manpage, or online docs at https://stedolan.github.io/jq
The problem is not the delimiter, but the assumption that each element of the array is already a valid JSON string.
The FAQ suggests using the following to encode a
bash
array as a JSON array:This assumes, however, that the elements of the array are themselves valid JSON values. A counterexample is
x=(1 $'a\nb' 2)
, where the second element contains a literal newline.I believe the only safe way to do this is with multiple calls to
jq
, building the array one element at a time:to ensure that each element of
x
is properly encoded before adding it to the array.The text was updated successfully, but these errors were encountered: