【问题标题】:Is it possible to get a list element based on a combination of multiple sub-list values?是否可以根据多个子列表值的组合来获取列表元素?
【发布时间】:2021-07-13 23:16:58
【问题描述】:

我有一个数据结构(由 kubectl 提供),格式如下,与集群中的一些自定义资源定义相关。当列表元素的.status.conditions 列表的条件为.type == "Ready".status == "False" 时,我想使用jq 或类似的方法返回.items[x].metadata.name

“业务案例”是“获取任何尚未准备好的自定义资源。”

我感觉数据的结构使这变得困难,但我想知道是否可以使用jq

非常感谢任何帮助!

{
    "apiVersion": "v1",
    "items": [
        {
            "metadata": {
                "name": "return-me"
            },
            "spec": {
                "region": "ap-southeast-2"
            },
            "status": {
                "conditions": [
                    {
                        "message": "Some Condition A",
                        "reason": "Supported",
                        "status": "True",
                        "type": "IsSupported"
                    },
                    {
                        "message": "Some Condition B",
                        "reason": "Shared",
                        "status": "True",
                        "type": "IsShared"
                    },
                    {
                        "message": "Some Condition C",
                        "reason": "Accepted",
                        "status": "False",
                        "type": "IsAccepted"
                    },
                    {
                        "status": "True",
                        "type": "OthersReady"
                    },
                    {
                        "status": "False",
                        "type": "Ready"
                    }
                ]
            }
        },
        {
            "metadata": {
                "name": "dont-return-me"
            },
            "spec": {
                "region": "ap-northeast-1"
            },
            "status": {
                "conditions": [
                    {
                        "message": "Some Condition A",
                        "reason": "Supported",
                        "status": "True",
                        "type": "IsSupported"
                    },
                    {
                        "message": "Some Condition B",
                        "reason": "Shared",
                        "status": "True",
                        "type": "IsShared"
                    },
                    {
                        "message": "Some Condition C",
                        "reason": "Accepted",
                        "status": "False",
                        "type": "IsAccepted"
                    },
                    {
                        "status": "True",
                        "type": "OthersReady"
                    },
                    {
                        "status": "True",
                        "type": "Ready"
                    }
                ]
            }
        },
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

【问题讨论】:

    标签: json shell select jq


    【解决方案1】:

    根据您的输入,过滤器

    .items[]
    | select( any(.status.conditions[];  .type == "Ready" and .status == "False") )
    | .metadata.name
    

    返回

    "return-me"
    

    ...这似乎充满希望:-)

    注意

    熟悉 SQL 的select 的人应该注意到,jq 的select 从输入流中选择元素,因此与 SQL 的 select 有很大不同,后者用于从表中的每个“记录”中选择“字段”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 2013-09-01
      • 1970-01-01
      • 2014-08-19
      • 2019-04-21
      • 2020-11-26
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多