如何使用基于多个键的值的jq过滤JSON数据,并在过滤器成功时显示一个特定属性?

时间:2018-01-01 14:43:05

标签: json jq

例如,我有这个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数据中。

现在我想过滤来自Tags []数组的某个key:value对匹配的PrivateIPAddress和InstanceType 。如果select(.Value == "qa" and .Value == databaseteam)匹配,我主要寻找类似的东西。换句话说,基于jq过滤器中定义的Tags数组的匹配条件,我想显示PrivateIPAddress和InstanceType。

我怎么能做到这一点?我已经尝试了地图,选择以及可能的各种以前的SO答案,github问题回复,但我不能让它工作。

谢谢,

1 个答案:

答案 0 :(得分:1)

这个问题有点令人困惑;例如,似乎预先假定.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"
]