The example here:
#!/bin/bash
declare -A rep_hostname
rep_hostname=( [test1]='172.1.1.1' [test2]='172.1.1.2' [test3]='172.1.1.3' )
json=$(
jo members="$(
jo -a "${rep_hostname[@]}" |
jq -c 'to_entries | map({ "_id": .key, "host": .value })'
)"
)
echo "$json"
outputs the following:
{
"members":[
{"_id":0,"host":"172.1.1.3:2701"},
{"_id":1,"host":"172.1.1.2:2701"},
{"_id":2,"host":"172.1.1.1:2701"}
]
}
How to make the following JSON?
{
"test1":"172.1.1",
"test2":"172.1.1.2",
"test3":"172.1.1.3"
}