'How to get child JsonNode with a Jayway JsonPath

Here're my use case:

Given a JsonNode (already parsed by Jackson), I'd like to provide a list of Jayway JsonPath to select sub JsonNodes that I'm interested.

i.e:

List<JsonNode> getSubNodes(JsonNode root, List<String> jsonPaths)

(Given the fact that my own library/Utils are built on Jackson API, so I want everything in memory is in Jackson model)

Is it doable?

Thanks in advance!



Solution 1:[1]

Unfortunately, at the present moment it is not possible to directly use pre-parsed JSON in form of Jackson JsonNode with Jayway JsonPath.

Either, a priori, parse it with the JSON providers mentioned in the comment of the original post or write the JSON node to String and re-parse with JsonPath.

For Example:

                Object o=JsonPath.read(new ObjectMapper()
                                        .writeValueAsString(n), 
                                        "$.classRelationships[*].targetClassName");

where 'n' is the JsonNode

The above is very very inefficient. I could not find any other way by going through the documentation.

If anyone has a better approach, I also would like to know. as I have the same requirement.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ironluca