The question is exactly the opposite of Constructing a json hash from a bash associative array. Given the following JSON:
{
"a": "z",
"b": "y",
"c": "x"
}
I would like to have an associative Bash array.
The question is exactly the opposite of Constructing a json hash from a bash associative array. Given the following JSON:
{
"a": "z",
"b": "y",
"c": "x"
}
I would like to have an associative Bash array.
A NUL-delimited stream is the safest approach:
input='{"a":"z","b":"y","c":"x"}'
declare -A data=()
while IFS= read -r -d '' key && IFS= read -r -d '' value; do
data[$key]=$value
done < <(jq -j 'to_entries[] | (.key, "\u0000", .value, "\u0000")' <<<"$input")
Note the use of jq -j, which suppresses quoting (like -r), but also suppresses the implicit newline between items, letting us manually insert a NUL instead.
See the discussion in https://github.com/stedolan/jq/issues/1271 (a ticket explicitly requesting a NUL-delimited output mode), wherein this idiom was first suggested.
You can do it like this :
while IFS=':' read k v
do
dict[$k]="$v"
done <<< "$( sed -E "s/(\"|,|{|})//g; /^ *$/d; s/^ *//g" input.txt )"
echo "The value of key 'a' is : ${dict[a]}"
Hope it helps you!
sed having the non-POSIX-defined -E argument. Your JSON has more than one key/value pair on a single line? Won't work. Your JSON has spaces inside the key names or values? Won't work. Your JSON has backslash-escaped literal quotes? Won't work. Your JSON has literal {s inside of the strings? They disappear. Etc.declare -A dict=( [a]="z" [b]="y" [c]="x" ). Obviously that's useless/absurd; an answer needs to be good generally, not only for the single sample input.map[string]string, so long as both the keys and values are C strings (which is to say, containing no NULs). jq, similarly, can handle any valid JSON -- it's a quite powerful programming language in its own right, and for that matter considerably more powerful than bash insofar as nested data structure manipulation is called for. Which is to say -- there are domain limitations implied by the tools selected, sure; but that's not a reason or excuse for adding your own arbitrary limitations on top, especially without disclosing them.