2016-08-01 26 views
0

私はjqには新しく、AWS ECSタスク定義を新しい値で更新するために使いたいと思っていました。 AWS cliは次のjsonレスポンスを返します。という名前のオブジェクトをという名前のプロパティに変更します。の値が "これはatestです"とCONFIG_URLを変更します。Jq update JSONキー:値に基づく値

{ 
    "family": "contentpublishing-task", 
    "volumes": [], 
    "containerDefinitions": [ 
    { 
     "environment": [ 
     { 
      "name": "TEST_ENV", 
      "value": "TEST" 
     }, 
     { 
      "name": "CONFIG_URL", 
      "value": "s3://stg-appcfg/config-20160729-1130.json" 
     } 
     ], 
     "name": "contentpublishing", 
     "mountPoints": [], 
     "image": "contentpublishing:blah", 
     "cpu": 512, 
     "portMappings": [ 
     { 
      "protocol": "tcp", 
      "containerPort": 8081, 
      "hostPort": 8080 
     } 
     ], 
     "memory": 256, 
     "essential": true, 
     "volumesFrom": [] 
    } 
    ] 
} 

は、次のクエリ

cat test.json | jq 'select(.containerDefinitions[0].environment[].name=="CONFIG_URL").value|="this is atest"' 2>&1 

を試みたが、次は戻ってきました。あなたが見ることができるように、追加の値キーが最も外側のjsonオブジェクトに追加されています。

{ 
    "family": "contentpublishing-task", 
    "volumes": [], 
    "containerDefinitions": [ 
    { 
     "environment": [ 
     { 
      "name": "TEST_ENV", 
      "value": "TEST" 
     }, 
     { 
      "name": "CONFIG_URL", 
      "value": "s3://stg-appcfg/config-20160729-1130.json" 
     } 
     ], 
     "name": "contentpublishing", 
     "mountPoints": [], 
     "image": "contentpublishing:blah", 
     "cpu": 512, 
     "portMappings": [ 
     { 
      "protocol": "tcp", 
      "containerPort": 8081, 
      "hostPort": 8080 
     } 
     ], 
     "memory": 256, 
     "essential": true, 
     "volumesFrom": [] 
    } 
    ], 
    "value": "this is atest" 
} 

答えて

3

値を設定する前に、対応する環境ノードを最初に選択する必要があります。あなたのクエリはコンテキストを変更しないので、それはルート項目に残っているので、新しい値をルートに追加することになります。ここ

$ jq --arg update_name "CONFIG_URL" --arg update_value "this is a test" \ 
'(.containerDefinitions[].environment[] | select(.name == $update_name)).value = $update_value' input.json 
0

はJQ Complex assignments

(
    .containerDefinitions[] 
| .environment[] 
| select(.name == "CONFIG_URL") 
| .value 
) |= "this is atest" 
を使用して解決します
関連する問題