I am generating a JSON file (input.json) that looks like this:
[
{
"name": "MINIMUM_NOTICE_PERIOD",
"value": "3"
},
{
"name": "SERVER_TIMEZONE",
"value": "Europe/London"
}
]
I have an existing JSON file (task.json) which is an AWS ECS task definition that looks like this:
{
"family": "test-container",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"cpu": 0,
"image": "test-image",
"name": "test-container",
"environment": [
{
"name": "CRON",
"value": "no"
},
{
"name": "LOG_CHANNEL",
"value": "stack"
}
]
}
],
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024"
}
I am trying to insert the extra values from the JSON file (input.json) into the task def json file (task.json)
I am using the command:
jq --argjson envVars "$(<input.json)" '.containerDefinitions[0].environment += [$envVars]' task.json > new-task.json
All was going well until the end when the result is:
{
"family": "test-container",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"cpu": 0,
"image": "test-image",
"name": "test-container",
"environment": [
{
"name": "CRON",
"value": "no"
},
{
"name": "LOG_CHANNEL",
"value": "stack"
},
[
{
"name": "MINIMUM_NOTICE_PE",
"value": "3"
},
{
"name": "SERVER_TIMEZONE",
"value": "Europe/London"
}
]
]
},
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024"
}
It is inserting the full contents of the file including opening and closing [] into the existing array, I was aiming for something more like this:
{
"family": "test-container",
"networkMode": "awsvpc",
"containerDefinitions": [
{
"cpu": 0,
"image": "test-image",
"name": "test-container",
"environment": [
{
"name": "CRON",
"value": "no"
},
{
"name": "LOG_CHANNEL",
"value": "stack"
},
{
"name": "MINIMUM_NOTICE_PE",
"value": "3"
},
{
"name": "SERVER_TIMEZONE",
"value": "Europe/London"
}
]
},
"requiresCompatibilities": [
"FARGATE"
],
"cpu": "512",
"memory": "1024"
}
I have also tried the command:
jq '.containerDefinitions[0].environment += $inputs' task.json --slurpfile inputs input.json
Running this gives the same resulting JSON output.
I haved tried just removing the brackets from the inpur.json file but doing so results in the following error from both jq commands:
jq: invalid JSON text passed to --argjson
jq: Bad JSON in --slurpfile inputs input.json
Anyone have an idea how to get the output I'm looking for? Any help is appriected as I've been pulling my hair out trying to figure out this last part.
input.json is already an array, so your jq filter could be as simple as:
.containerDefintions[0].environments += $envVars
Using --slurpfile has the effect of wrapping the contents of the file in an array, so your second try would have to involve $inputs[0]
.
Since input.json
is already a file, I would suggest using a --argfile
(even though it is officially deprecated); or, with a bit more work, you could use an invocation along the lines of jq ... input.json task.json