JSONPath通过子子值获取父元素的ID [英] JSONPath get the id of a parent element by a sub-child value

查看:509
本文介绍了JSONPath通过子子值获取父元素的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下JSON,我想通过对子元素的等号文本比较来获取父元素的id字段:

Given the following JSON I want to get the id field of the parent by an equals text compare of a sub-child element:

{
    "datapoints": [{
            "id": "default.1",
            "definedBy": "default/0.1",
            "featureValues": {
                "bui.displayname": "Health status",
                "bui.visibility": "normal",
                "default.access": "r",
                "default.basetype": "text",
                "default.description": "Aggregated health status",
                "default.format": "text/plain",
                "default.name": "health_status",
                "default.restriction": "re:(OK|WARN|ERROR|UNKNOWN)"
            }
        }, {
            "id": "kdl.240",
            "definedBy": "kdl/0.9",
            "featureValues": {
                "bui.displayname": "Delta K",
                "bui.visibility": "normal",
                "default.access": "rw",
                "default.basetype": "real",
                "default.description": "Delta K",
                "default.name": "Delta_K",
                "default.privacy": "false",
                "default.restriction": "b32"
            }
        }
    ]
}

我的首要目标是通过子子文本比较来获取正确的数据点,例如:

My first goal is to get the correct data point by a sub-child text compare like:

$['datapoints'][*]['featureValues'][?(@['default.name']=='Delta_K')]

当我在 http://jsonpath.com/上对其进行测试时,它似乎不起作用 为了获得所有数据点,我成功地使用了它:

It seems not to work when I test it on http://jsonpath.com/ To get all the data points I used this successfully:

$['datapoints'][*]['featureValues']['default.name']

我的目标是获取与featureValues子元素default.name等于Delta_K的数据点的id值.在示例中,该名称为kdl.240.

My goal is to get the id value of the data point with the featureValues child element default.name is equal Delta_K. In the example this would be kdl.240.

推荐答案

我只能使用以下方法解决问题的第一部分:

I could only solve the first part of my question by using:

$['datapoints'][*][?(@['default.name']=='Delta_K')]

在研究期间,我发现jsonpath不支持获取已过滤节点的父级.在 http://www.baeldung.com/guide-to-的第7章结论"中jayway-jsonpath 写道:

During my research I found that jsonpath does not support to get the parent of a filtered node. In Chapter 7 "Conclusion" of http://www.baeldung.com/guide-to-jayway-jsonpath it's written:

尽管JsonPath有一些缺点,例如缺少用于到达 parent 或同级节点的运算符,但它在许多情况下都非常有用.

Although JsonPath has some drawbacks, such as a lack of operators for reaching parent or sibling nodes, it can be highly useful in a lot of scenarios.

另外,SO帖子也无济于事.

Also further SO posts couldn't help me.

  • Getting parent of matched element with jsonpath
  • Using jsonpath to get parent node

这篇关于JSONPath通过子子值获取父元素的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆