0

I've been banging my head against the wall with this one a little bit, partially because I'm new to jq.

I have JSON output from one command (terraform output -json). I want to take this data, rename some keys, and replace keys in a file with the values.

Example source data:

{
    "auth_token_secret_id": {
        "sensitive": false,
        "type": "string",
        "value": "<redacted>"
    },
    "ecr_url": {
        "sensitive": false,
        "type": "string",
        "value": "<redacted>"
    }
}

Example template data to be updated (terraform_files/task_definition.json):

[
  {
    "name": "command_server",
    "image": "${image_path}",
    "cpu": 512,
    "memory": 1024,
    "environment": [
      {
        "name": "AUTH_TOKEN_ID",
        "value": "${auth_token_id}"
      }
    ]
  }
]

I'm trying to get ecr_url to be set as the image_path and auth_token_secret_id as auth_token_id.

I was doing something like this:

echo "$(sed -E 's/\${([a-z_]*)}/$\1/g' terraform_files/task_definition.json \
| paste -s -)" \
| jq --arg image_path "$(terraform output ecr_url)"  \
--arg auth_token_id "$(terraform output auth_token_secret_id)" \
'.[0].image = $image_path | (.[0].environment[] | select(.name == "COMMAND_AUTH_TOKEN_ID") | .value = $auth_token_id)'

But that was only getting me output for the second replacement and omitting the rest of the target template. I definitely do not understand how jq works for cases like this. Can anyone help?

I have a sed command doing this for now, but I'd prefer jq in case the format of things changes. I also just want to learn more about jq.

(It won't let me tag the question with jq because I'm not cool enough. It's like high school all over again)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.