Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONPath resolver for Java objects

How can I get a value from an Java object instead from a JSON string by applying a JSONPath expression?


I receive a Java object that is created from a JSON string (via Jackson, no way to influence it):

public class MyJSONInputClass {
    private String foo;
    private int[] bar = { 1, 5, 9 };
    private OtherClass baz;
    ....
}

I further have some JSONPath expressions as Java Strings reflecting values in the object (they might be much more complex):

"$.foo"
"$.bar[5]"
"$.baz.someProperty"

I want to resolve those expressions using the resulting java object (instance of MyJSONInputClass, after unmarshalling):

public Object resolve(MyJSONInputClass input, String expression) {
    ...
}
like image 655
Sebastian Barth Avatar asked Jan 02 '15 14:01

Sebastian Barth


People also ask

What is JsonPath in Java?

JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is available in many programming languages such as Javascript, Python and PHP. JsonPath allows you to compile a json path string to use it many times or to compile and apply in one single on demand operation.

What is JsonPath used for?

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.

What is the difference between JSON and 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.


1 Answers

I use ObjectMapper from Jackson to create a Map<String, Object> from the given Java object (containing other maps for properties not parseable as primitive types). Then JSONPath can read it and evaluate the expression.

public Object resolve(Object input, String expression) {
    // Get the mapper with default config.
    ObjectMapper mapper = new ObjectMapper();

    // Make the object traversable by JSONPath.
    Map<String, Object> mappedObject = mapper.convertValue(input, Map.class);

    // Evaluate that expression
    Object output = JsonPath.read(mappedObject, expression);

    return output;
}

Dependencies to include:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>1.2.0</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.4</version>
</dependency>

Some notes:

  • Works for hierarchical objects.
  • Not tested for circular structures.
like image 176
Sebastian Barth Avatar answered Oct 18 '22 04:10

Sebastian Barth