I'm looking to build object using jq and add object key to dynamic key and I could not figure out. Here is my sample script:
#!/bin/bash -e
environments=('development' 'stage' 'production')
regions=('us-east-1' 'us-west-2')
tree='{}'
for environment in "${environments[@]}"
do
echo "${environment}"
# Or do something else with environment
tree="$(jq --arg jqEnvironment "${environment}" '. | .[$jqEnvironment] = {}' <<< "${tree}")"
for region in "${regions[@]}"
do
echo "${region}"
# Or do something with region
tree="$(jq --arg jqEnvironment "${environment}" --arg jqRegion "${region}" '. | .[$jqEnvironment] | .[$jqRegion] = {}' <<< "${tree}")"
done
done
jq . <<< "${tree}"
actual outputs
{
"us-west-2": {}
}
But what I want is this
{
"development": {
"us-east-1": {},
"us-west-2": {}
},
"stage": {
"us-east-1": {},
"us-west-2": {}
},
"production": {
"us-east-1": {},
"us-west-2": {}
}
}
I could not figure out, please help!