I have a json file test.json with below format:
{
"environments":
{
"egress": [
]
}
}
I want to add objects under egress field with multiple variable values. I have written a bash script test.sh with below code snippet:
egress_port=(80,443)
egress_value=(test,test1)
egress_protocol=(http,https)
IFS=','
read -a port_array <<< "$egress_port"
read -a value_array <<< "$egress_value"
read -a protocol_array <<< "$egress_protocol"
echo "${port_array[@]}"
echo "${value_array[@]}"
echo "${protocol_array[@]}"
cat test.json |
jq '.environments.egress[.environments.egress| length] |= . + {
"port": "${port_array[@]}",
"type": "external",
"value": "${value_array[@]}",
"protocol": "${protocol_array[@]}"
}'
I want json to be updated in below format:
{
"environments":
{
"egress": [
{
"port": "80",
"type": "external",
"value": "test",
"protocol": "http"
},
{
"port": "443",
"type": "external",
"value": "test1",
"protocol": "https"
}
]
}
}
Can someone help me here..