1

We are trying to get jqv1.5 64bit to parse an array then echo out the result.

Do we have the correct jq syntax?

#!/bin/bash
declare -a nameArr 
nameArr=("Leia" "Darth Vader" "Anakin" "Han Solo" "Yoda")
jq -c -n  "$nameArr" 

gives error

jq: error: syntax error, unexpected $end (Unix shell quoting issues?) at <top-level>, line 1:
Leia,    
jq: 1 compile error
3
  • How should your output look like? Commented Jan 3, 2018 at 17:55
  • Note, btw, that jq -c -n "$nameArr" passes only the first element of the array, so it runs jq -c -n "Leia," with your original input. There isn't a good way to pass a shell array (which can contain any list of valid C strings) inside a single C string. Commented Jan 3, 2018 at 17:59
  • @CharlesDuffy, you are right with the comma. I did add that as a typo when creating the question. Thanks for the catch, I have updated to actual array Commented Jan 3, 2018 at 18:03

1 Answer 1

7

To use a sigil that can't possibly exist as a value in a shell array, NUL is an appropriate choice.

nameArr=( "Leia" "Darth Vader" "Anakin" "Han Solo" "Yoda" )
printf '%s\0' "${nameArr[@]}" | jq -csR 'split("\u0000")'

...properly emits:

["Leia","Darth Vader","Anakin","Han Solo","Yoda"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.