0

I have the following json:

{
  "taskDefinition": {
    "containerDefinitions": [
      {
        "memoryReservation": 1040,
        "mountPoints": [

        ],
        "name": "staging-web1",
        "image": "1234567.dkr.ecr.us-west-2.amazonaws.com\/staging:staging-web",
        "essential": true,
        "environment": [
          {
            "name": "REVISION",
            "value": ""
          },
          {
            "name": "RELEASE_VERSION",
            "value": ""
          },
          {
            "name": "ENVIRONMENT",
            "value": ""
          }
        ],
        "logConfiguration": {
          "logDriver": "awslogs",
          "options": {
            "awslogs-region": "us-west-2",
            "awslogs-group": "\/ecs\/staging",
            "awslogs-stream-prefix": "ecs"
          }
        },
        "portMappings": [

        ],
        "cpu": 0,
        "volumesFrom": [

        ]
      },
      {
        "memoryReservation": 1040,
        "mountPoints": [

        ],
        "name": "s-staging-nosql1",
        "image": "1234567.dkr.ecr.us-west-2.amazonaws.com\/staging:staging-nosql",
        "essential": true,
        "environment": [
          {
            "name": "REVISION",
            "value": ""
          },
          {
            "name": "RELEASE_VERSION",
            "value": ""
          },
          {
            "name": "ENVIRONMENT",
            "value": ""
          }  
        ],
        "logConfiguration": {
          "logDriver": "awslogs",
          "options": {
            "awslogs-region": "us-west-2",
            "awslogs-group": "\/ecs\/staging",
            "awslogs-stream-prefix": "ecs"
          }
        },
        "portMappings": [

        ],
        "cpu": 0,
        "volumesFrom": [

        ]
      },
      {
        "memoryReservation": 1040,
        "mountPoints": [

        ],
        "name": "s-staging-db1",
        "image": "1234567.dkr.ecr.us-west-2.amazonaws.com\/staging:staging-db",
        "essential": true,
        "environment": [
          {
            "name": "REVISION",
            "value": ""
          },
          {
            "name": "RELEASE_VERSION",
            "value": ""
          },
          {
            "name": "ENVIRONMENT",
            "value": ""
          }  
        ],
        "logConfiguration": {
          "logDriver": "awslogs",
          "options": {
            "awslogs-region": "us-west-2",
            "awslogs-group": "\/ecs\/staging",
            "awslogs-stream-prefix": "ecs"
          }
        },
        "portMappings": [

        ],
        "cpu": 0,
        "volumesFrom": [

        ]
      }
    ]
  }
}

What I am trying to do with python is to adjust the REVISION, RELEASE_VERSION, and ENVIRONMENT under each environment in containerDefinitions.

I'm a total novice when it comes to python but what I am trying is as follows:

NEW_CONTAINER_DEF=$(echo "${PREVIOUS_TASK_DEF}" | python <(cat <<-EOF
import sys, json
json_data = json.load(sys.stdin)
for item in json_data['taskDefinition']['containerDefinitions']:
    if json_data.get(['environment']['name'])  == "REVISION":
         item['environment']['value']  = "myrevision"
print json.dumps(json_data)
EOF
))

Needless to say the code above doesn't work. Can anyone with more python experience help me to understand how I can iterate and update the values?

Thanks!

1 Answer 1

3

Note that calling json.load is simply serializing the json into a python dictionary. You can just modify that dictionary accordingly.

It appears that first you would like to iterate over all taskDefinition.containerDefinitions, then you would like to iterate over all the environments in those definitions and set the environment value if the environment name is REVISION. See this:

for item in json_data["taskDefinition"]["containerDefinitions"]:
    for env in item["environment"]:
         if env["name"] == "REVISION":
             env["value"] = "myrevision"
print json.dumps(json_data, indent=4)
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. I've been banging my head off the wall trying all sorts of permutations trying to get this to work. I have experience programming in other languages but I'm a python newbie so I couldn't figure out where I was going wrong. Thank You!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.