Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAQ contains error regarding import of bash arrays #1620

Open
chepner opened this issue Mar 9, 2018 · 4 comments
Open

FAQ contains error regarding import of bash arrays #1620

chepner opened this issue Mar 9, 2018 · 4 comments
Labels

Comments

@chepner
Copy link

chepner commented Mar 9, 2018

The FAQ suggests using the following to encode a bash array as a JSON array:

jq -n --argjson args "$(printf '\"%s\"\n' "${x[@]}" | jq -s)" '$args'

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.

@pkoppstein
Copy link
Contributor

The FAQ explicitly says:

This approach is applicable so long as none of the values contains a newline character.

It then goes on to provide a generic approach that uses NUL to avoid the iteration that your proposed solution entails.

@chepner
Copy link
Author

chepner commented Mar 9, 2018

$ 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.

@pkoppstein
Copy link
Contributor

The problem (now fixed) was the example forgot to include the -s option:

$ x=(1 $'a\nb' 2)
$  jq -n --argjson args "$(printf '%s\0' "${x[@]}" | jq -Rsc 'split("\u0000")')" '$args'
[
  "1",
  "a\nb",
  "2"
]

Thanks for keeping us honest!

@trailstrider
Copy link

Another candidate to close per #2305 ?

@itchyny itchyny added the docs label Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants