Using JsonPath in Java for JSON Data Extraction and Validation
This article demonstrates how to integrate the JsonPath library into a Java project, explains its syntax for querying JSON structures, and provides practical code examples for extracting authors, prices, and entire nodes from a sample JSON document.
The author describes a testing platform similar to Postman and introduces JsonPath as a solution for richer validation and multi‑case chaining in the second phase of development.
Adding the jar
compile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0'JSON data
JSONObject json = JSON.parseObject("{" +
" \"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}," +
" {\"category\": \"fiction\", \"author\": \"Herman Melville\", \"title\": \"Moby Dick\", \"isbn\": \"0-553-21311-3\", \"price\": 8.99}," +
" {\"category\": \"fiction\", \"author\": \"J. R. R. Tolkien\", \"title\": \"The Lord of the Rings\", \"isbn\": \"0-395-19395-8\", \"price\": 22.99}" +
" ]," +
" \"bicycle\": {\"color\": \"red\", \"price\": 19.95}" +
" }," +
" \"expensive\": 10," +
" \"ss\": [32,32,4,23]" +
"}");JsonPath syntax
JsonPath expressions start with $ for the root object or array. They can use dot notation (e.g., $.store.book[0].title) or bracket notation (e.g., $['store']['book'][0]['title']) to navigate JSON structures, similar to XPath for XML.
API
/**
* Creates a new JsonPath and applies it to the provided Json object
* @param json a json object
* @param jsonPath the json path
* @param filters filters to be applied to the filter place holders [?] in the path
* @param <T> expected return type
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(Object json, String jsonPath, Predicate... filters) {
return parse(json).read(jsonPath, filters);
}Getting all book authors
Object read = JsonPath.read(json, "$.store.book[*].author");
output(read);The same query can return a List directly, yielding the identical author list.
List read = JsonPath.read(json, "$.store.book[*].author");
output(read);If an incompatible return type is used, a ClassCastException will be thrown.
Getting all authors (recursive)
$...author
$.store..priceThese expressions retrieve all author values regardless of depth and all price values under store respectively.
Getting all information under a node
$.store.*
$.ss.*These queries return the complete objects under store and the array ss.
The article concludes by inviting readers to follow future posts on handling JSON arrays.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
