1

I have a json as mentioned in the first section and i want to add an element("image": "<IMAGE1_NAME>",) in the json as specified in the expected output.

I tired to use jq like this:

'.containerDefinitions += {"operation":"delete"}'

but this seem to not work. can anyone help me the jq command to achieve the result i want.

{
"taskDefinitionArn": "arn:aws",
"containerDefinitions": [
    {
        "name": "dev-service",
        "cpu": 0,
        "portMappings": [
            {
                "containerPort": 8080,
                "hostPort": 8080,
                "protocol": "tcp"
            }
        ],
        "essential": true,
        "environment": [
            {
                "name": "message",
                "value": "Hi, EMC!!"
            }
        ]
        }
    }
]

}

expected output :

{
"taskDefinitionArn": "arn:aws:ecs:us-west-2:619867110810:task-definition/emc-dev-backend:30",
"containerDefinitions": [
    {
        "name": "dev-service",
        "image": "<IMAGE1_NAME>",
        "cpu": 0,
        "portMappings": [
            {
                "containerPort": 8080,
                "hostPort": 8080,
                "protocol": "tcp"
            }
        ],
        "essential": true,
        "environment": [
            {
                "name": "message",
                "value": "Hi, EMC!!"
            }
        ]
        }
    }
]

}

Abhi.G
  • 1,801
  • 5
  • 20
  • 35

2 Answers2

2

Since it's an array, we'll need some sort of loop, use

.containerDefinitions[] += { "image":"IMAGE1_NAME" }

To add { "image":"IMAGE1_NAME" } on each item in the containerDefinitions key


JqPlay Demo

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

Your input JSON is not formatted correctly. Assuming your actual input is

{
  "taskDefinitionArn": "arn:aws",
  "containerDefinitions": [
    {
      "name": "dev-service",
      "cpu": 0,
      "portMappings": [
        {
          "containerPort": 8080,
          "hostPort": 8080,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "environment": [
        {
          "name": "message",
          "value": "Hi, EMC!!"
        }
      ]
    }
  ]
}

which would mean that containerDefinitions is an array, not an object. Thus, to add an object as another array item, go with

jq '.containerDefinitions += [{"image":"<IMAGE1_NAME>"}]'
{
  "taskDefinitionArn": "arn:aws",
  "containerDefinitions": [
    {
      "name": "dev-service",
      "cpu": 0,
      "portMappings": [
        {
          "containerPort": 8080,
          "hostPort": 8080,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "environment": [
        {
          "name": "message",
          "value": "Hi, EMC!!"
        }
      ]
    },
    {
      "image": "<IMAGE1_NAME>"
    }
  ]
}

Demo

If, however, you wanted to merge that object into one of the array items (if to all of them, use [] instead of [0]), go with

jq '.containerDefinitions[0] += {"image":"<IMAGE1_NAME>"}'
{
  "taskDefinitionArn": "arn:aws",
  "containerDefinitions": [
    {
      "name": "dev-service",
      "cpu": 0,
      "portMappings": [
        {
          "containerPort": 8080,
          "hostPort": 8080,
          "protocol": "tcp"
        }
      ],
      "essential": true,
      "environment": [
        {
          "name": "message",
          "value": "Hi, EMC!!"
        }
      ],
      "image": "<IMAGE1_NAME>"
    }
  ]
}

Demo

pmf
  • 24,478
  • 2
  • 22
  • 31