2017-04-13 9 views
1

AWSリソースの管理を自動化するためのbashスクリプトを作成しています。私はaws-clijqを使用しています。これまでのことは素晴らしかったです。ネストされた値に基づくjqを使用したフィルタ配列

自分のタグにカスタムタグを付けてタグ付けしています。状況によっては、カスタムタグのKeyValueの両方に基づいてリソースのリストをフィルタリングしたいと考えています。しかし、私はそれを行うために簡潔なjqクエリを試して問題を抱えています。

ので、例えば、私のEC2インスタンスのための(トリミング)JSON出力が似ている場合:

[ 
    { 
     "PublicIpAddress": "11.22.33.44", 
     "PrivateIpAddress": "55.66.77.88", 
     "Tags": [ 
      { 
       "Value": "live199.blah.com", 
       "Key": "Name" 
      }, 
      { 
       "Value": "live-standalone", 
       "Key": "hc-class" 
      } 
     ] 
    } 
] 
[ 
    { 
     "PublicIpAddress": "111.222.333.444", 
     "PrivateIpAddress": "555.666.777.888", 
     "Tags": [ 
      { 
       "Value": "staging99.blah.com", 
       "Key": "Name" 
      }, 
      { 
       "Value": "staging-standalone", 
       "Key": "hc-class" 
      } 
     ] 
    } 
] 

...と私はTags.Key == "hc-class"Tags.Value = "staging-standalone"が、私はそれをどのように行うのか、エントリを見つける必要があります簡潔な方法でjq

大変助かりました。

+1

あなたはaws CLIから直接JMESPATHを使ってそれを行うことができます。質問はhttp://stackoverflow.com/questions/43354116/using-jmespath-and-aws-ec2-describe-instances-to-output-multiple-タグ値 - どのようにして作業するのかを具体的に説明する値 –

+0

「エントリー」とはどういう意味ですか? 「タグ」は配列なので、包含基準は不明です。あなたが期待した結果を出せば助けになるかもしれません。 – peak

+0

[複数のオブジェクトをマッチングしてJSONオブジェクトリストをjqでフィルタリングする]の可能な複製(http://stackoverflow.com/questions/33973816/filtering-json-object-list-with-jq-by-matching-multiple-objects) –

答えて

2

を以下に示すように、次のフィルタは出力を生成します。

.[] | select(any(.Tags[]; .Key == "hc-class" and .Value == "staging-standalone")) 

出力:

{ 
    "PublicIpAddress": "111.222.333.444", 
    "PrivateIpAddress": "555.666.777.888", 
    "Tags": [ 
    { 
     "Value": "staging99.blah.com", 
     "Key": "Name" 
    }, 
    { 
     "Value": "staging-standalone", 
     "Key": "hc-class" 
    } 
    ] 
} 
+0

素晴らしい。それはまさに私が必要なものです。乾杯! –

0

私はJQに非常に新しいですが、私は、私はここで解決策持っているように感じる:与えられた入力と

https://jqplay.org/s/DljtxNX_72

select((.[0].Tags[1].Key) == "hc-class" and (.[0].Tags[1].Value) == "staging-standalone") 
関連する問題