简体   繁体   English

如何根据多个键的值使用jq过滤JSON数据,并在过滤成功的情况下显示一个特定属性?

[英]How to filter JSON data with jq based on values from multiple keys and show one particular attribute if the filter succeeds?

For example, I have this JSON data: 例如,我有以下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"
}

Similar data is scattered throughout the JSON data as obtained from aws ec2 describe-instances command. aws ec2 describe-instances命令获得的类似数据分散在整个JSON数据中。

Now I want to filter PrivateIPAddress and InstanceType only if some key: value pair from the Tags[] array match. 现在, 在标签[]数组中的某些键:值对匹配时,我才想过滤PrivateIPAddress和InstanceType。 I am primarily looking for something like, if select(.Value == "qa" and .Value == databaseteam) match. 我主要在寻找类似的东西,如果select(.Value == "qa" and .Value == databaseteam)匹配。 In other words, based out of match criterion from the Tags array defined in the jq filter, I want to show PrivateIPAddress and InstanceType. 换句话说,基于jq过滤器中定义的Tags数组的不匹配条件,我想显示PrivateIPAddress和InstanceType。

How I can achieve this? 我该如何实现? I have tried map, select and possibly all kinds of previous SO answers, github issue replies but I can't get this to work. 我已经尝试了map,选择了可能的所有先前的SO答案,github问题答复,但我无法使它正常工作。

Thanks, 谢谢,

The question is a bit confusing; 这个问题有点令人困惑。 for example, it seems to presuppose that .Value can simultaneously be equal to both "qa" and "databaseteam". 例如,似乎假设.Value可以同时等于“ qa”和“ databaseteam”。 In fact, in your sample data, there is no "instance" for which(.Value == "qa" and .Key == "databaseteam") either, so for the sake of this illustration, I'll assume you meant 实际上,在您的示例数据中,也没有哪个“实例”(.Value ==“ qa”和.Key ==“ databaseteam”),因此,为了便于说明,我假设您的意思是

select(.Value == "qa" and .Key == "environment")

However, if you meant something else, perhaps select(.Value == "qa" or .Value == "databaseteam") , you can easily adapt the following, which produces the results shown below. 但是,如果您要使用其他含义,例如select(.Value == "qa" or .Value == "databaseteam") ,则可以轻松调整以下内容,从而产生如下所示的结果。

.Instances[]
| .PrivateIpAddress as $PIPA
| .InstanceType as $IT
| .Tags[]
| select(.Value == "qa" and .Key == "environment")
| [$PIPA, $IT]

Transcript 抄本

$ jq -f program.jq data.json
[
  "bb",
  "t2.micro"
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM