简体   繁体   English

JQ:获取元素ID(如果包含密钥)

[英]JQ: Getting element id if it contains a key

I am trying to get a list of elements in "Parameters" that have a "Default" key: 我正在尝试获取“参数”中具有“默认”键的元素的列表:

{
"Parameters" : {
    "Ecosystem": {
        "Type": "String",
        "Description": "Ecosystem to deploy the resources in to",
        "MinLength": "1"
    },
    "InstanceTenancy": {
        "Type": "String",
        "Description": "EC2 Instance Tenancy",
        "Default": "default",
        "AllowedValues": [
            "default", "dedicated"
        ]
    },
    "InstanceSecurityGroups": {
        "Type": "List<AWS::EC2::SecurityGroup::Id>",
        "Description": "EC2 Instance Security Groups",
        "MinLength": "1"
    },
    "InstanceAmi": {
        "Type": "AWS::EC2::Image::Id",
        "Description": "AMI to deploy to the EC2 instances",
        "Default": "ami-11223344"
    }
}

} }

The closest I am getting is jq '.Parameters | map_values(has("Default"))' 我得到的最接近的是jq '.Parameters | map_values(has("Default"))' jq '.Parameters | map_values(has("Default"))'

{
  "Ecosystem": false,
  "InstanceTenancy": true,
  "InstanceSecurityGroups": false,
  "InstanceAmi": true
}

Is there a way I can get just a list of keys which match this filter? 有没有一种方法可以让我仅获取与此过滤器匹配的键列表? eg 例如

"InstanceTenancy"
"InstanceAmi"

Using to_entries allows a fundamentally simple solution: 使用to_entries允许一个根本上简单的解决方案:

.Parameters
| to_entries[]
| select(.value | has("Default"))
| .key

But your approach can also be made to work: 但是您的方法也可以起作用:

.Parameters
| map_values(has("Default"))
| keys_unsorted[] as $k
| select(.[$k])
| $k

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

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