JSONPath 通过子子值获取父元素的 id

JSONPath get the id of a parent element by a sub-child value

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

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

我的第一个目标是通过子子文本比较获得正确的数据点,例如:

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

我在http://jsonpath.com/上测试好像不行 要获取我成功使用的所有数据点:

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

我的目标是让featureValues子元素default.name的数据点的id值等于Delta_K。在示例中,这将是 kdl.240.

我只能通过以下方式解决问题的第一部分:

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

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

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 帖子也帮不了我。

  • Using jsonpath to get parent node
#cat jsonData.json | jq ‘.datapoints[].featureValues | select .default.name == 'Delta_K') | .id’

另见: https://github.com/adriank/ObjectPath/issues/70