【问题标题】:JQ: Getting element id if it contains a keyJQ:获取元素ID,如果它包含一个键
【发布时间】:2019-01-08 20:51:21
【问题描述】:

我正在尝试获取“参数”中具有“默认”键的元素列表:

{
"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"
    }
}

}

我得到的最接近的是jq '.Parameters | map_values(has("Default"))'

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

有没有办法只获取与此过滤器匹配的键列表?例如

"InstanceTenancy"
"InstanceAmi"

【问题讨论】:

    标签: json select key extract jq


    【解决方案1】:

    使用to_entries 可以得到一个基本简单的解决方案:

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

    但你的方法也可以奏效:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多