Search code examples
javajsonpath

Does Jayway support filter on children level, return the parent node?


I wonder if it is possible to filter on the child level complex objects (not simple string), and retrieve those parent nodes whose children matches the predicts.

Take the bookstore example from Jayway's github project: https://github.com/jayway/JsonPath#path-examples.

If I would change "author" to a more complex object like following:

  "authors": [
    {
      "firstName": "Nigel",
      "lastName": "Rees"
    },
    {
      "firstName": "Evelyn",
      "lastName": "Waugh"
    }
  ]

With that, how can I get back "all books that were written by someone with last name of 'Waugh' " ?

I tried to play with Json Evaluator (http://jsonpath.herokuapp.com/) use something like $.store.book[?(@.authors[?(@.lastName == 'Waugh')])] , but it seems like the filter doesn't work. I am guessing Jayway does not allow a nested predicates. If that's the case, would there be any workaround for this?

Also if there is any other Java implementation of JsonPath that could come into help, I would love to learn it.

Thank you in advance.


Solution

  • Looks like it can't be achieved without providing the index value for parent array (i.e. authors). I am aware that it may not fully resolve the problem. I just thought of confirming the limitation when there is array inside another array.

    $.store.book[?(@.authors[0].lastName == 'Waugh')]