Search code examples
javajsonjsonpath

Strategies for iterating through JsonPath array


I just started exploring JsonPath today. I want to explore not just what's possible to do with it, but some effective strategies.

For instance, let's say I have to iterate through an array contained within one element in the json string.

I'm using the "store" example from https://github.com/jayway/JsonPath#path-examples .

To get the list of books itself, I would imagine I could do something like this:

List<?> allBooks    = JsonPath.<List<?>>read(context, "$.store.book");

Does it make sense to think about it this way?

It's the logic for iterating through this that I'm uncertain about.

I would have thought I could define a "Book" pojo and then do something like this:

    for (int ctr = 0; ctr < allBooks.size(); ++ ctr) {
        Book book   = JsonPath.<Book>read(context, ".[" + ctr + "]");
        System.out.println("book[" + book + "]");
    }

However, this doesn't work. The "read" method at this point returns a JSONArray.

The last line in the code sample at https://github.com/jayway/JsonPath#what-is-returned-when is close to what I'm looking at, but this requires parsing the json in every iteration. It seems like the "DocumentContext" class has "read" methods that can take a type parameter, but not "JsonPath".

What are some reasonable strategies for navigating something like this?


Solution

  • JSON path will just return you a list of Maps as you've no doubt already seen. You need a way to tell it how to map these values to an object - for this you will need a custom configuration. There are other providers like Gson etc., but I've only used Jackson.

    Configuration configuration = Configuration
            .builder()
            .jsonProvider(new JacksonJsonProvider())
            .mappingProvider(new JacksonMappingProvider())
            .build();
    

    The second step is to specify generic type information with a TypeRef and pass it along when reading the tag.

    List<Book> allBooks = JsonPath.using(configuration)
            .parse(context)
            .read("$.store.book", new TypeRef<List<Book>>() {});
    

    As a result you get a nice list of Book objects.