Skip to content

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):


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

public Object resolve(MyJSONInputClass input, String expression) {



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 =, expression);

    return output;

Dependencies to include:



Some notes:

  • Works for hierarchical objects.
  • Not tested for circular structures.
6 People found this is helpful