Provides JSON DOM construction, encoding/decoding (serialization), retrieval, JsonPath queries, JsonSchema validation, and more.
<dependency>
<groupId>org.noear</groupId>
<artifactId>snack4-jsonpath</artifactId>
<version>4.0.36</version>
</dependency>
Snack-JsonPath draws inspiration from JavaScript's var declaration for all variables and XML DOM's "everything is a Node" design philosophy. All underlying data is represented as ONode, which stands for "One node" - capable of representing any type and converting to any type.
- Emphasizes document tree construction and manipulation capabilities
- High-performance JSON Path queries (significantly faster than jayway.jsonpath). Compatible with both
jayway.jsonpathand the IETF JSONPath (RFC 9535) standard (switchable viaoptions). Delivering a robust JsonPath experience for the next decade. - Supports
Json schemavalidation - Supports partial
json5features (unquoted keys, comments, etc.) - Prioritizes no-argument constructors + field encoding/decoding (reduces risks of injection-triggered actions)
- Supports Java 8 to Java 25
Dependency packages:
| Package | Description |
|---|---|
org.noear:snack4 |
Provides json dom construction and encoding/decoding support |
org.noear:snack4-jsonpath |
Provides json path query support |
org.noear:snack4-jsonschema |
Provides json schema validation support |
Open source repository:
Documentation:
1. Recent Updates
- Added
snack4Json Feature.Read_TrimString feature - Added
snack4JsonReader.iterableNext method (supports JSON streaming, particularly adapted for LLM stream output) - Added
snack4JsonReader.readLast method - Added
snack4ONode field type deserialization support - Added
snack4custom annotation retrieval support - Added
snack4Options.then method for chained building - Added
snack4CodecLib.patternCreators, patternDecoders, patternEncoders deduplication handling - Added
snack4MapperLib.schemaPatternMappers, typePatternMappers deduplication handling - Added
snack4AtomicBoolean, AtomicLong, AtomicInteger support - Added
snack4Optional built-in codec support (also customizable) - Added
snack4ONode:delete method to assist with jsonpath deletion - Added
snack4-jsonschemaJsonSchema default value generation - Added
snack4-jsonschematype mapping mechanism, supporting wrapper or delivery types like Future, Optional - Optimized
snack4Iterable support (replacing previous Collection) - Optimized
snack4deserialization to automatically remove '@type' property declaration - Optimized
snack4-jsonpathlogical expression compatibility to support space-less "a=='a'" - Optimized
snack4-jsonschemaOptional type handling - Changed
snack4JsonReader.streamRead to readNext (former marked as deprecated) - Changed
snack4-jsonpathJsonPath.delete to return bool (previously void) - Fixed
snack4ONode.ofBean and ofJson throwing exception when passed null - Fixed
snack4Json BeanDecoder not restoring array.item when null (incorrectly filtered) - Fixed
snack4-jsonschemaJsonSchema TypeRule.getSchemaTypeName not recognizing initial node as null (was recognized as undefined) - Fixed
snack4-jsonpathJsonPathProvider:delete(root, path) out-of-bounds issue when deleting multiple array indexes
2. JSONPath Syntax Reference
| Syntax Element | Description |
|---|---|
$ |
Root node identifier |
@ |
Current node identifier (valid only in filter selectors) |
[<selectors>] |
Child segment: selects zero or more child nodes of a node |
.name |
Shorthand for ['name']
|
.* |
Shorthand for [*]
|
..[<selectors>] |
Descendant segment: selects zero or more descendants of a node |
..name |
Shorthand for ..['name']
|
..* |
Shorthand for ..[*]
|
'name' |
Name selector: selects named child of an object |
* |
Wildcard selector: selects all children of a node |
3 |
Index selector: selects array index item (0-based) |
0:100:5 |
Array slice selector: start:end:step for arrays |
?<logical-expr> |
Filter selector: selects specific items using logical expressions |
fun(@.foo) |
Filter function: calls function in filter expression (IETF standard) |
.fun() |
Aggregate function: used as a segment (jayway style) |
Filter selector syntax reference:
| Syntax | Description | Precedence |
|---|---|---|
(...) |
Grouping | 5 |
name(...) |
Function extension | 5 |
! |
Logical NOT
|
4 |
==,!=,<,<=,>,>=
|
Relational comparison operators | 3 |
&& |
Logical AND
|
2 |
| `\ | \ | ` |
IETF JSONPath (RFC 9535) Standard Defined Operators (Supported)
| Operator | Description | Example |
|---|---|---|
== |
Left equals right (note: 1 does not equal '1') | $[?(@.a == 1)] |
!= |
Left does not equal right | $[?(@.a != 1)] |
< |
Left is less than right | $[?(@.a < 1)] |
<= |
Left is less than or equal to right | $[?(@.a <= 1)] |
> |
Left is greater than right | $[?(@.a > 1)] |
>= |
Left is greater than or equal to right | $[?(@.a >= 1)] |
jayway.jsonpath Extended Operators (Supported)
| Operator | Description | Example |
|---|---|---|
=~ |
Left matches regex | [?(@.s =~ /foo.*?/i)] |
in |
Left exists in right | [?(@.s in ['S', 'M'])] |
nin |
Left does not exist in right | |
subsetof |
Left is a subset of right | [?(@.s subsetof ['S', 'M', 'L'])] |
anyof |
Left has at least one intersection with right | [?(@.s anyof ['M', 'L'])] |
noneof |
Left has no intersection with right | [?(@.s noneof ['M', 'L'])] |
size |
Left (array or string) size should match right | $[?(@.s size @.expected_size)] |
empty |
Left (array or string) should be empty | $[?(@.s empty false)] |
IETF JSONPath (RFC 9535) Standard Defined Functions (Supported)
| Function | Description | Parameter Type | Result Type |
|---|---|---|---|
length(x) |
Length of string, array, or object | Value | Number |
count(x) |
Size of node list | Node List | Number |
match(x,y) |
Regex full match | Value, Value | Boolean |
search(x,y) |
Regex substring match | Value, Value | Boolean |
value(x) |
Value of single node in node list | Node List | Value |
jayway.jsonpath Functions (Supported)
| Function | Description | Output Type |
|---|---|---|
length() |
Length of string, array, or object | Integer |
min() |
Find minimum value in current numeric array | Double |
max() |
Find maximum value in current numeric array | Double |
avg() |
Calculate average of current numeric array | Double |
stddev() |
Calculate standard deviation of current numeric array | Double |
sum() |
Calculate sum of current numeric array | Double |
keys() |
Get property key set of current object | Set<E> |
concat(X) |
Concatenate an item or collection with current array into new array | like input |
append(X) |
Append an item or collection to current path's output array | like input |
first() |
Return first element of current array | Depends on array element type |
last() |
Return last element of current array | Depends on array element type |
index(X) |
Return element at index X in current array. X can be negative (counting from end) | Depends on array element type |
Top comments (0)