【问题标题】:JSONPath query - parse object of objectJSONPath 查询 - 解析对象的对象
【发布时间】:2022-01-05 18:48:43
【问题描述】:

我正在尝试编写一个基于条件选择特定对象的 JsonPath 查询。

我想选择名称包含“mydrive”和“hosts”数组但没有值的所有卷。你能帮我获得好的 JSONPath 查询吗?

有了这个json输出

{
    "purefa_info": {
            "volumes": {
                "DATASTORE": {
                    "bandwidth": null,
                    "host_encryption_key_status": "none",
                    "hosts": [
                        {
                            "host": "esxi1",
                            "lun": 251
                        },
                        {
                            "host": "esxi2",
                            "lun": 251
                        }
                    ]
                },
                "RC1Clone": {
                    "bandwidth": null,
                    "host_encryption_key_status": "none",
                    "hosts": []
                },
                "RC2Clone": {
                    "bandwidth": null,
                    "host_encryption_key_status": "none",
                    "hosts": []
                },
                "mydrive-0d32e3799a": {
                    "bandwidth": null,
                    "host_encryption_key_status": "none",
                    "hosts": []
                },
                "mydrive-0e35cb6455": {
                    "bandwidth": null,
                    "host_encryption_key_status": "none",
                    "hosts": [
                        {
                            "host": "esxi1",
                            "lun": 251
                        },
                        {
                            "host": "esxi2",
                            "lun": 251
                        }
                    ]
                },
                "mydrive-55c61ab79c": {
                    "bandwidth": null,
                    "host_encryption_key_status": "none",
                    "hosts": []
                }   
         }
    }
}

我想要:

[
  "$['purefa_info']['volumes']['mydrive-0d32e3799a']",
  "$['purefa_info']['volumes']['mydrive-55c61ab79c']"
]

你能帮帮我吗? 谢谢

【问题讨论】:

  • 可以编辑您的问题以显示您的尝试吗?
  • 您是否也可以添加标签以指示您正在使用的编程语言或实现/库。
  • 我在jsonpath.com 上尝试了这个 $.purefa_info[?(@.volumes contains 'pxcloud')].['volumes'][?(@.hosts == 0)],但它不匹配。
  • 您的 JSON 中不存在 pxcloud。你确定你在帖子中包含了完整的 JSON 吗? jsonpath.com 也不支持 contains
  • @AkshayG,你是对的,但即使我的驱动器模式很好,同样的问题。我会尝试在 Ansible 中使用 json 查询。

标签: jsonpath


【解决方案1】:

如果您使用的是JSONPath-Plus 中使用的https://jsonpath.com/,则可以使用以下表达式。

$.purefa_info.*[?(@property.match(/mydrive/) && @.hosts == 0)]

注意:@property 在原始spec 中不存在。

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2015-11-13
    • 2017-12-15
    相关资源
    最近更新 更多