使用 jq 如何查询和替换数组中的值

using jq how to query and replace value within an array

如何查询和替换 SMT_PORT_3306_TCP_ADDR 的值。

我试过了

echo $task_definition | jq -r '.taskDefinition.containerDefinitions[0].environment[] | select(.name=="SMT_PORT_3306_TCP_ADDR")| .value = "myvalue" '

我得到的输出

{
  "name": "SMT_PORT_3306_TCP_ADDR",
  "value": "myvalue"
}

我没有得到完整的json

输入Json:

{
    "taskDefinition": {
        "taskDefinitionArn": "some value",
        "containerDefinitions": [
            {
                "name": "common-api-img",
                "environment": [
                    {
                        "name": "SERVER_API_TIMEOUT_SUBSCRIPTIONS_CANCEL_REQUEST",
                        "value": "false"
                    },
                    {
                        "name": "SMT_PORT_3306_TCP_ADDR",
                        "value": "valueToReplace"
                    }
                ],
                "mountPoints": [],
                "volumesFrom": []
            }
        ],
        "revision": 65,
        "volumes": [],
        "status": "ACTIVE"
    }
}

没有顶层的预期输出taskDefinition值:

{
   "taskDefinitionArn":"some value",
   "containerDefinitions":[
      {
         "name":"common-api-img",
         "environment":[
            {
               "name":"SERVER_API_TIMEOUT_SUBSCRIPTIONS_CANCEL_REQUEST",
               "value":"false"
            },
            {
               "name":"SMT_PORT_3306_TCP_ADDR",
               "value":"myvalue"
            }
         ],
         "mountPoints":[
            
         ],
         "volumesFrom":[
            
         ]
      }
   ],
   "revision":65,
   "volumes":[
      
   ],
   "status":"ACTIVE"
}

|=if 结合使用。

jq '.taskDefinition.containerDefinitions[0].environment[]
        |= if   .name == "SMT_PORT_3306_TCP_ADDR"
           then .value = "myvalue"
           else .
           end'