Skip to content

Commit 41a6a08

Browse files
committed
Issue #129 Expand README JsonPath example
1 parent 9d9c1db commit 41a6a08

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,16 +399,46 @@ https://goessner.net/articles/JsonPath/
399399
```java
400400
import jdk.sandbox.java.util.json.*;
401401
import json.java21.jsonpath.JsonPath;
402+
import json.java21.jsonpath.JsonPathStreams;
402403

403404
JsonValue doc = Json.parse("""
404-
{"store": {"book": [{"author": "A"}, {"author": "B"}]}}
405+
{"store": {"book": [
406+
{"author": "Nora Quill", "title": "Signal Lake", "price": 8.95},
407+
{"author": "Jae Moreno", "title": "Copper Atlas", "price": 12.99},
408+
{"author": "Marek Ilyin", "title": "Paper Comet", "price": 22.99}
409+
]}}
405410
""");
406411

407-
JsonPath path = JsonPath.parse("$.store.book[*].author");
408-
var authors = path.query(doc);
412+
var authors = JsonPath.parse("$.store.book[*].author")
413+
.query(doc)
414+
.stream()
415+
.map(JsonValue::string)
416+
.toList();
417+
418+
System.out.println("Authors count: " + authors.size()); // prints '3'
419+
System.out.println("First author: " + authors.getFirst()); // prints 'Nora Quill'
420+
System.out.println("Last author: " + authors.getLast()); // prints 'Marek Ilyin'
421+
422+
var cheapTitles = JsonPath.parse("$.store.book[?(@.price < 10)].title")
423+
.query(doc)
424+
.stream()
425+
.map(JsonValue::string)
426+
.toList();
427+
428+
var priceStats = JsonPath.parse("$.store.book[*].price")
429+
.query(doc)
430+
.stream()
431+
.filter(JsonPathStreams::isNumber)
432+
.mapToDouble(JsonPathStreams::asDouble)
433+
.summaryStatistics();
434+
435+
System.out.println("Total price: " + priceStats.getSum());
436+
System.out.println("Min price: " + priceStats.getMin());
437+
System.out.println("Max price: " + priceStats.getMax());
438+
System.out.println("Avg price: " + priceStats.getAverage());
409439
```
410440

411-
See AGENTS.md for detailed guidance including logging configuration.
441+
See `json-java21-jsonpath/README.md` for JsonPath operators and more examples.
412442

413443
## Augmented Intelligence (AI) Welcomed
414444

0 commit comments

Comments
 (0)