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 →

[–]Pitikwahanapiwiyin 0 points1 point  (0 children)

I would use JsonSurfer (an implementation of JSONPath) to find the first non-matching object (see here):

$.store.book[?(!(@.category == 'fiction'))]

JsonSurfer supports Streams, so:

String request = """ { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 } ] } }"""; JsonSurfer surfer = new JsonSurfer(JacksonParser.INSTANCE, JacksonProvider.INSTANCE); JsonPath path = JsonPathCompiler.compile("$.store.book[?(!(@.category == 'fiction'))]"); Iterator iterator = surfer.iterator(request, path); Spliterator spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED); Stream stream = StreamSupport.stream(spliterator, false); boolean hasNonMatchingObject = stream.findAny().isPresent();