Ebrahim Moshaya
Ebrahim Moshaya

Reputation: 817

JSON JQ Find Replace Value

I am trying to update "image_id" value in a json structure. Using the below command, how do I change ami-d8cf5cab to ami-a4df7gah So far, I have tried this

cat cog.test.tfstate | jq -r '.modules[].resources[] | select(.type == "aws_launch_configuration") | select(.primary.attributes.name_prefix == "pmsadmin-lc-")'

The JSON data is

{
  "type": "aws_launch_configuration",
  "primary": {
    "id": "pmsadmin-lc-v47thk6rcrdgza6dujfzjatmju",
    "attributes": {
      "associate_public_ip_address": "false",
      "ebs_block_device.#": "0",
      "ebs_optimized": "false",
      "enable_monitoring": "true",
      "ephemeral_block_device.#": "0",
      "iam_instance_profile": "cog-test-pmsadmin",
      "id": "pmsadmin-lc-v47thk6rcrdgza6dujfzjatmju",
      "image_id": "ami-d8cf5cab",
      "instance_type": "t2.small",
      "key_name": "cog-test-internal",
      "name": "pmsadmin-lc-v47thk6rcrdgza6dujfzjatmju",
      "name_prefix": "pmsadmin-lc-",
      "root_block_device.#": "0",
      "security_groups.#": "4",
      "security_groups.1893851868": "sg-7ee7bf1a",
      "security_groups.2774384192": "sg-e2e7bf86",
      "security_groups.2825850029": "sg-86e6bee2",
      "security_groups.3095009517": "sg-f4e7bf90",
      "spot_price": "",
      "user_data": "ed03ac6642af8c97562b065c0b37f211b58ad0a2"
    }
  }
}

Upvotes: 9

Views: 23986

Answers (1)

hek2mgl
hek2mgl

Reputation: 157990

Use the |= operator to assign to a property:

jq -r '.modules[].resources[] | select(.type == "aws_launch_configuration") | select(.primary.attributes.name_prefix == "pmsadmin-lc-")| .primary.attributes.image_id |= "ami-a4df7gah"

Upvotes: 20

Related Questions