Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

jsonpath

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')]

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']

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.

like image 813
Bruno Bieri Avatar asked Aug 07 '17 08:08

Bruno Bieri


People also ask

What is difference between JSON & JSONPath?

JSONPath creates a uniform standard and syntax to define different parts of a JSON document. JSONPath defines expressions to traverse through a JSON document to reach to a subset of the JSON. This topic is best understood by seeing it in action. We have created a web page which can help you evaluate a JSONPath.

What is Jayway JSONPath?

Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation.

What is JSONPath and XPath?

JSONPath is a query language for JSON, similar to XPath for XML. XPath expression can be used to extract information from an XML document by evaluating given expression. JSONPath Guide. XPath Guide.

What is JSONPath read?

JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document. You use a JSONPath expression to traverse the path to an element in the JSON structure.


2 Answers

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

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

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:

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.

Also further SO posts couldn't help me.

  • Getting parent of matched element with jsonpath
  • Using jsonpath to get parent node
like image 83
Bruno Bieri Avatar answered Dec 08 '22 22:12

Bruno Bieri


The following code is working for me on https://jsonpath.com :

$.datapoints[?(@.featureValues['default.name']=='Delta_K')].id
like image 40
Brice Avatar answered Dec 08 '22 20:12

Brice