![](/img/trans.png)
[英]How to filter a JSON object based on a condition and then just select few keys from the filtered object using the JQ parser
[英]How to filter JSON data with jq based on values from multiple keys and show one particular attribute if the filter succeeds?
例如,我有以下JSON數據:
{
"Instances": [
{
"Monitoring": {
"State": "disabled"
},
"PublicDnsName": "ec2-aa.compute-1.amazonaws.com",
"State": {
"Code": 16,
"Name": "running"
},
"EbsOptimized": false,
"LaunchTime": "2017-08-14T17:29:54.000Z",
"PublicIpAddress": "aa",
"PrivateIpAddress": "bb",
"ProductCodes": [],
"VpcId": "vpc-xx",
"StateTransitionReason": "",
"InstanceId": "i-f21c76a0",
"ImageId": "ami-xx",
"PrivateDnsName": "ip-bb.ec2.internal",
"KeyName": "blah",
"SecurityGroups": [
{
"GroupName": "mygroup",
"GroupId": "sg-xx"
}
],
"ClientToken": "",
"SubnetId": "subnet-xx",
"InstanceType": "t2.micro",
"NetworkInterfaces": [
{
"Status": "in-use",
"MacAddress": "",
"SourceDestCheck": true,
"VpcId": "vpc-xx",
"Description": "",
"NetworkInterfaceId": "eni-xx",
"PrivateIpAddresses": [
{
"PrivateDnsName": "ip-bb.ec2.internal",
"PrivateIpAddress": "aa",
"Primary": true,
"Association": {
"PublicIp": "aa",
"PublicDnsName": "ec2-bb-1.amazonaws.com",
"IpOwnerId": "amazon"
}
}
],
"PrivateDnsName": "ip-aa.ec2.internal",
"Attachment": {
"Status": "attached",
"DeviceIndex": 0,
"DeleteOnTermination": true,
"AttachmentId": "eni-attach-b11454cb",
"AttachTime": "2014-07-02T19:24:19.000Z"
},
"Groups": [
{
"GroupName": "mygroup",
"GroupId": "sg-xx"
}
],
"Ipv6Addresses": [],
"OwnerId": "aa",
"PrivateIpAddress": "aa",
"SubnetId": "subnet-bb",
"Association": {
"PublicIp": "aa",
"PublicDnsName": "ec2-aa-1.amazonaws.com",
"IpOwnerId": "amazon"
}
}
],
"SourceDestCheck": true,
"Placement": {
"Tenancy": "default",
"GroupName": "",
"AvailabilityZone": "us-east-1e"
},
"Hypervisor": "xen",
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"Status": "attached",
"DeleteOnTermination": true,
"VolumeId": "vol-4fb6ed03",
"AttachTime": "2014-07-02T19:24:23.000Z"
}
}
],
"Architecture": "x86_64",
"RootDeviceType": "ebs",
"RootDeviceName": "/dev/sda1",
"VirtualizationType": "hvm",
"Tags": [
{
"Value": "qa",
"Key": "environment"
},
{
"Value": "database",
"Key": "system"
},
{
"Value": "databaseteam",
"Key": "team"
}
],
"AmiLaunchIndex": 0
}
],
"ReservationId": "r-xx",
"Groups": [],
"OwnerId": "xx"
}
從aws ec2 describe-instances
命令獲得的類似數據分散在整個JSON數據中。
現在, 僅在標簽[]數組中的某些鍵:值對匹配時,我才想過濾PrivateIPAddress和InstanceType。 我主要在尋找類似的東西,如果select(.Value == "qa" and .Value == databaseteam)
匹配。 換句話說,基於jq過濾器中定義的Tags數組的不匹配條件,我想顯示PrivateIPAddress和InstanceType。
我該如何實現? 我已經嘗試了map,選擇了可能的所有先前的SO答案,github問題答復,但我無法使它正常工作。
謝謝,
這個問題有點令人困惑。 例如,似乎假設.Value可以同時等於“ qa”和“ databaseteam”。 實際上,在您的示例數據中,也沒有哪個“實例”(.Value ==“ qa”和.Key ==“ databaseteam”),因此,為了便於說明,我假設您的意思是
select(.Value == "qa" and .Key == "environment")
但是,如果您要使用其他含義,例如select(.Value == "qa" or .Value == "databaseteam")
,則可以輕松調整以下內容,從而產生如下所示的結果。
.Instances[]
| .PrivateIpAddress as $PIPA
| .InstanceType as $IT
| .Tags[]
| select(.Value == "qa" and .Key == "environment")
| [$PIPA, $IT]
$ jq -f program.jq data.json
[
"bb",
"t2.micro"
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.