Practical Guide to JsonPath: Filtering JSON Data with Operators and Functions
This article explains how to use JsonPath in Java to filter JSON data, covering operators, functions, and practical examples such as checking values in arrays, subsets, size constraints, and empty checks, with complete code snippets for each case.
Continuing from the previous three parts, this post focuses on the JsonPath API for filtering JSON data. JsonPath operators (e.g., == , > , =~ ) follow language conventions, while functions such as in , nin , and anyof provide more expressive queries.
The first part of the article briefly mentions that the operators are straightforward and concentrates on demonstrating the usage of functions.
json data
The sample JSON object is extended with two new fields: page and pages .
JSONObject json = JSON.parseObject("{" +
" \"store\": {" +
" \"book\": [" +
" {" +
" \"category\": \"reference\",\n \"author\": \"Nigel Rees\",\n \"title\": \"Sayings of the Century\",\n \"page\": \"D\",\n \"pages\": [\"S\",\"X\",\"G\"],\n \"price\": 8.95" +
" }," +
" {" +
" \"category\": \"fiction\",\n \"author\": \"Evelyn Waugh\",\n \"title\": \"Sword of Honour\",\n \"page\": \"A\",\n \"pages\": [\"A\",\"B\"],\n \"price\": 12.99" +
" }," +
" {" +
" \"category\": \"fiction\",\n \"author\": \"Herman Melville\",\n \"title\": \"Moby Dick\",\n \"isbn\": \"0-553-21311-3\",\n \"page\": \"B\",\n \"pages\": [\"E\",\"F\"],\n \"price\": 8.99" +
" }," +
" {" +
" \"category\": \"fiction\",\n \"author\": \"J. R. R. Tolkien\",\n \"title\": \"The Lord of the Rings\",\n \"isbn\": \"0-395-19395-8\",\n \"page\": \"C\",\n \"pages\": [\"C\",\"D\"],\n \"price\": 22.99" +
" }" +
" ]," +
" \"bicycle\": {" +
" \"color\": \"red\",\n \"price\": 19.95" +
" }" +
" }," +
" \"expensive\": 10," +
" \"ss\": [32,32,4,23]" +
"}");Field value in a specific array
Using the in function to select books whose page is either 'A' or 'C':
JsonPath expression: $.store.book[?(@.page in ['A','C'])]
Object read = JsonPath.read(json, "$.store.book[?(@.page in ['A','C'])]");
output(JSONArray.parseArray(read.toString()));The console output shows the matching JSON objects.
Field value not in a specific array
Using the nin (not in) function to select books whose page is not 'A' or 'C':
JsonPath expression: $.store.book[?(@.page nin ['A','C'])]
Object read = JsonPath.read(json, "$.store.book[?(@.page nin ['A','C'])]");
output(JSONArray.parseArray(read.toString()));Subset check
The subsetof operator verifies that the array field pages is a subset of a given list:
JsonPath expression: $.store.book[?(@.pages subsetof ['A','B','C'])]
Object read = JsonPath.read(json, "$.store.book[?(@.pages subsetof ['A','B','C'])]");
output(JSONArray.parseArray(read.toString()));Array length and string length validation
The size operator can validate the length of arrays or strings. Examples:
Array length: $.store.book[?(@.pages size 3)]
String length: $.store.book[?(@.author size 16)]
Object read = JsonPath.read(json, "$.store.book[?(@.pages size 3)]");
output(JSONArray.parseArray(read.toString()));
Object read2 = JsonPath.read(json, "$.store.book[?(@.author size 16)]");
output(JSONArray.parseArray(read2.toString()));Empty check
When checking for empty values, the size 0 expression can be used as a reliable alternative.
Finally, the article includes a brief promotion of the FunTester public account and links to other popular articles.
FunTester
10k followers, 1k articles | completely useless
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.