I have the below JSON where I need modify few elements values and also extract only the contents inside containerDefinitions while retaining the square bracket.
The below script works in replacing the required value and extract the json inside the square brack inside containerDefinitions but leaves out the square bracket. Is there additinal command that can retain the square brackets.
echo $TASK_DEFINITION | jq '.taskDefinition.containerDefinitions[0] | ( .environment[] |= if .name == "SMT_PORT_3306_TCP_ADDR" then .value = "myvalue" elif .name == "SERVER_API_TIMEOUT_SUBSCRIPTIONS_CANCEL_REQUEST" then .value = "myvalue" else . end) | .logConfiguration.options."awslogs-group" = "myvalue" ' > ${TASK_DEFINITION_PATH}/${SERVICE_NAME}2-task-definition.json
Input JSON
{
"taskDefinition":{
"taskDefinitionArn":"some value",
"containerDefinitions":[
{
"name":"common-api-img",
"environment":[
{
"name":"SERVER_API_TIMEOUT_SUBSCRIPTIONS_CANCEL_REQUEST",
"value":"false"
},
{
"name":"SMT_PORT_3306_TCP_ADDR",
"value":"valueToReplace"
}
],
"mountPoints":[
],
"volumesFrom":[
]
}
],
"revision":65,
"volumes":[
],
"status":"ACTIVE"
}
}
expected output
[
{
"name":"common-api-img",
"environment":[
{
"name":"SERVER_API_TIMEOUT_SUBSCRIPTIONS_CANCEL_REQUEST",
"value":"myvalue"
},
{
"name":"SMT_PORT_3306_TCP_ADDR",
"value":"myvalue"
}
],
"mountPoints":[
],
"volumesFrom":[
]
}
]