If you really cannot use a suitable JSON parser, for example jq [1] , try awk solution :
Bash 4.x:
readarray -t values < <(awk -F\" 'NF>=3 {print $4}' myfile.json)
Bash 3.x:
IFS=$'\n' read -d '' -ra values < <(awk -F\" 'NF>=3 {print $4}' myfile.json)
All the property values ββare stored here in the Bash ${values[@]} array, which you can check with declare -p values .
These solutions have limitations:
- each property should be on a separate line,
- all values ββmust be double,
- built-in escaped double quotes are not supported.
All of these limitations support the recommendation to use the correct JSON parser.
Note. The following alternative solutions use the Bash 4.x + readarray -t values , but they also work with the Bash 3.x alternative, IFS=$'\n' read -d '' -ra values .
grep + cut combination : one grep will not work (if you are not using GNU grep - see below), but adding cut helps:
readarray -t values < <(grep '"' myfile.json | cut -d '"' -f4)
GNU grep : using -P to support PCRE, which support \K , to discard everything that has been agreed so far (a more flexible alternative to asserting appearance), and also to look at the -general statements ( (?=...) ):
readarray -t values < <(grep -Po ':\s*"\K.+(?="\s*,?\s*$)' myfile.json)
Finally, here's a clean Bash solution (3.x +) :
What makes this a viable alternative in terms of performance is that no external utilities are called in each iteration of the loop; however, for large input files, a solution based on external utilities will be much faster.
#!/usr/bin/env bash declare -a values
[1] Here what reliable jq solution will look like (Bash 4.x):
readarray -t values < <(jq -r '.[]' myfile.json)