I have a json variable which looks like this:
{
"status": "closed",
"host": "host1-availabe_zone_A"
}
{
"status": "closed",
"host": "host2-availabe_zone_B"
}
{
"status": "closed",
"host": "host3-availabe_zone_A"
}
I am trying to convert it to a hash-like data structure (associate array in zsh/bash) to get number of hosts in each availabe zone. The preferred result will be this based on the data above:
availabe_zone_A -> 2
availabe_zone_B -> 1
I got some problem when trying to iterate the json variable.
declare -A az_to_number_of_open_hosts
for host in $(jq . <<< $hosts_list)
do
az=$(rev <<< $(echo $host | jq .host) | cut -d. -f3 | rev)
((az_to_number_of_open_hosts[$az]++))
done
The line to extract available zone is because the host format is always blabla123213.az.domain.com
.
The code does not work since it seems the variable host
refers to each line of json string rather than a json entry. Is there a recommended way to iterate each json entry?