简体   繁体   中英

Using jq to find and replace elements from Json sub collection

I am trying to parse convert Json Task definition with new image.

I want to change the "image": "docker.org/alpha/alpha-app-newgen:12.2.3" value in Json to "image": "docker.org/alpha/alpha-app-newgen:12.5.0" or any other version dynamically.

below is my Json task def:

{
  "taskDefinition": {
    "family": "ing-stack",
    "volumes": [
      {
        "host": {
          "sourcePath": "/tmp/nginx/elb.conf"
        },
        "name": "volume-0"
      }
    ],
    "containerDefinitions": [
      {
        "dnsSearchDomains": [],
        "environment": [
          {
            "name": "API_SECRET",
            "value": "ING-SECRET"
          },
          {
            "name": "API_KEY",
            "value": "AVERA-CADA-VERA-KEY"
          }
        ],
        "readonlyRootFilesystem": false,
        "name": "ing-stg",
        "links": [],
        "mountPoints": [],
        "image": "docker.org/alpha/alpha-app-newgen:12.2.3",
        "privileged": false,
        "essential": true,
        "portMappings": [
          {
            "protocol": "tcp",
            "containerPort": 19000,
            "hostPort": 19000
          }
        ],
        "dockerLabels": {}
      },
      {
        "dnsSearchDomains": [],
        "environment": [
          {
            "name": "NG_PROXY",
            "value": "ing"
          }
        ],
        "readonlyRootFilesystem": false,
        "name": "web",
        "links": [
          "identity-ng"
        ],
        "mountPoints": [
          {
            "sourceVolume": "volume-0",
            "readOnly": false,
            "containerPath": "/etc/nginx/conf.d/default.conf"
          }
        ],
        "image": "docker.org/alpha/alpha-ui:6.4.7",
        "portMappings": [
          {
            "protocol": "tcp",
            "containerPort": 443,
            "hostPort": 443
          },
          {
            "protocol": "tcp",
            "containerPort": 80,
            "hostPort": 80
          }
        ],
        "memory": 512,
        "command": [
          "sh",
          "prep-run-nginx.sh"
        ],
        "dockerLabels": {}
      }
    ],
    "revision": 136
  }
}

I need to get the same structure back with new value for the image.

I tried the following jq '. | select(.containerDefinitions[].image | contains("'$new_img_no_ver'") ) | .image |= "my new image"' jq '. | select(.containerDefinitions[].image | contains("'$new_img_no_ver'") ) | .image |= "my new image"' jq '. | select(.containerDefinitions[].image | contains("'$new_img_no_ver'") ) | .image |= "my new image"' , but its adding to the end of the JSON.

Can anybody tell me how to achieve this.

Here are two potential solutions. Other variants are of course possible.

walk/1

If you don't want to be bothered with the details of exactly where the relevant "image" tag is located, consider using walk/1 :

Invocation:

jq --arg old "docker.org/alpha/alpha-app-newgen:12.2.3" --arg new "HELLO" -f update.jq input.json

update.jq:

walk(if type == "object" and .image == $old then .image=$new else . end)

If your jq does not have walk/1, then consider updating or getting its jq definition by googling: jq def walk

Targeted update

Invocation: as above

update.jq:

.taskDefinition.containerDefinitions[].image |= (if . == $old then $new else . end)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM