Issue
I am having problems parsing a JSON file using jq from a .sh script. When the object contains a space, shell splits it into two. Can i escape the space somehow?
echo $(jq '.' ../ext/js/language/languages.json) | jq -c '.[1]'
returns:
{"iso":"en","language":"English","flag":"πΊπΈπ¬π§","exampleText":"don't read"}
which is what i want (languages.json contains an array of such objects)
From shell:
entries=($(jq -c '.[]' ../ext/js/language/languages.json))
echo "[1] ${entries[1]}"
echo "[2] ${entries[2]}"
[1] {"iso":"en","language":"English","flag":"πΊπΈπ¬π§","exampleText":"don't
[2] read"}
Solution
You could convert each item into a JSON-encoded string using @json
, and escape that for shell using @sh
. Then, On the shell's side, use declare -a
to declare the indexed Bash array.
unset entries
declare -a entries="($(
jq -r '.[] | @json | @sh' ../ext/js/language/languages.json
))"
echo "[1] ${entries[1]}"
[1] {"iso":"en","language":"English","flag":"πΊπΈπ¬π§","exampleText":"don't read"}
Answered By - pmf Answer Checked By - Cary Denson (WPSolving Admin)