Mastering JsonPath with Regex: Filter JSON Arrays and Validate Nodes
This tutorial demonstrates how to use JsonPath combined with regular expressions to filter JSON arrays, perform case‑insensitive matches, and discuss the limitations of regex validation on node values, providing concrete Java code examples and console outputs.
JSON Data Example
The article starts with a sample JSON object representing a store with a list of books and a bicycle, slightly modified from the official demo.
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]
}");Filtering Arrays with Regex
Using JsonPath, you can filter the book array by matching the author field with a regular expression. The expression $..book[?(@.author =~ /.*Rees/)] (or the equivalent $.store.book[?(@.author =~ /.*Rees/)]) selects books whose author name contains "Rees".
Object read = JsonPath.read(json, "$.store.book[?(@.author =~ /.*Rees/)]");
output(JSONArray.parseArray(read.toString()));Console output shows the matching JSON object:
INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
> {"author":"Nigel Rees","price":8.95,"category":"reference","title":"Sayings of the Century"}
Process finished with exit code 0Case‑Insensitive Matching
JsonPath supports the /i flag to ignore case. The expression $.store.book[?(@.author =~ /.*REES/i)] matches the same author regardless of letter case.
Object read = JsonPath.read(json, "$.store.book[?(@.author =~ /.*REES/i)]");
output(JSONArray.parseArray(read.toString()).isEmpty()); // returns false
Object read2 = JsonPath.read(json, "$.store.book[?(@.author =~ /.*Rees/i)]");
output(JSONArray.parseArray(read2.toString()));Console output confirms the case‑insensitive match returns the expected JSON object.
Regex Validation of Node Values
The article notes that direct regex validation of a node's value is not currently supported by JsonPath. The author mentions plans to wrap functionality to add this capability.
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.
