I have a json response as shown below :
[
{"id":10,
"list_file":["/var/a.txt",
"/dev/b.txt"]}
]
I need to extract values of list_file and store it shell variable as an array. I tried doing it looping through and reading the values.
#!/bin/bash
x=()
while read -r value
do
#echo "$value"
x+=("$value")
done < <(jq -r '.[] | .list_file' input.json)
But the extracted values in the array contains quotes, brackets and comma too.
[
"/var/a.txt",
"/dev/b.txt"
]
Could you please help me modify the code so that the array contains only the entries /var/a.txt and /dev/b.txt. Also, I tried readarray and map, but they won't work on Mac Osx. Any help would be really appreciated.
In order to handle all values properly, you need to use the declare
command to incorporate the output of jq
into an array assignment.
Here's some input with some corner cases to worry about: whitespace, a newline, and a glob character.
$ cat input.json
[
{"id":10,
"list_file":[
"/var/a b.txt",
"/dev/c\nd.txt",
"*"]}
]
A jq
command that extracts and outputs properly quoted strings for use by the shell:
$ $ jq -r '.[] | .list_file[] | @sh' input.json
'/var/a b.txt'
'/dev/c
d.txt'
'*'
And a shell command that can make use of the output:
$ declare -a "x=($(jq -r '.[] | .list_file[] | @sh' input.json))"
Proof that it worked properly:
$ printf '==%s==\n' "${x[@]}"
==/var/a b.txt==
==/dev/c
d.txt==
==*==
You can use the @tsv
string formatter in combination with --raw-output
/-r
option and split the output to bash
array on tabs:
$ IFS=$'\t'
$ x=($(jq -r '.[] | .list_file | @tsv' input.json))
$ for xx in "${x[@]}"; do echo "$xx"; done
/var/a.txt
/dev/b.txt
For -r
will strip the outer quotes (useful for making jq
filters talk to non-JSON-based systems), and @tsv
will output an array as a series of tab-separated strings (tabs, newlines, etc. will be escaped).
Alternatively, you use the @sh
filter which outputs an array as a series of space-separated strings. However, to interpret such output, you have to eval
uate it:
$ eval "x=($(jq -r '.[] | .list_file | @sh' input.json))"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With