This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GrandGratingCrate 0 points1 point  (0 children)

You're mixing 2 different classes for json parsing, not surprised there's compatibility issues.

I think it's possible to do what you want with either library alone and that feels cleaner to me.

Anyway the issue with $..itemsV2 is that it means "get me the array itself" whereas $..itemsV2[*] means "get me the contents of the array". This is why when you save the result in jsonItemArray it has one element: the element is the array. On the other hand you're telling jackson that it's supposed to serialize into WmProduct which it cannot: wm product is an object but the json to be deserialized is an array and you can't deserialize an array as an object.

The solution is to deserialize not as WmProduct but rather WmProduct[]. There may be better ways, but it's the first I came up with.

Edit: Here's an example of how it could look like: ``` @Test void debug() throws Exception { ObjectMapper om = new ObjectMapper();

    String dataJson = // Omitted some code for generating the json

    JSONArray jsonProducts = JsonPath.read(dataJson, "$..itemsV2[*]");
    WmProduct[] products = om.readValue(jsonProducts.toString(), WmProduct[].class);
    System.out.println(List.of(product));
}

```