2017-04-13 6 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

JMESPATH를 사용하여 aws CLI에서 바로 수행 할 수 있습니다. http://stackoverflow.com/questions/43354116/using-jmespath-and-aws-ec2-describe-instances-to-output-multiple- tag-values ​​그 일을하는 방법에 대한 자세한 설명이있는 것 –

+0

"entry"란 무엇을 의미합니까? "태그"는 배열이기 때문에 포함 기준이 명확하지 않습니다. 기대 한 결과를 주면 도움이 될 것입니다. – peak

+0

[여러 객체를 일치시켜 jq를 사용하여 JSON 객체 목록 필터링] 가능한 중복 (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")