public abstract class JSONPath
extends java.lang.Object
JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document.
Example usage:
// Parse JSON string and extract value using JSONPath
String json = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";
Object result = JSONPath.extract(json, "$.name");
// result = "John"
// Evaluate JSONPath on an object
Map<String, Object> map = new HashMap<>();
map.put("name", "John");
map.put("age", 30);
Object result = JSONPath.eval(map, "$.name");
// result = "John"
// Set value using JSONPath
JSONPath.set(map, "$.name", "Jane");
// map.get("name") = "Jane"
// Remove value using JSONPath
JSONPath.remove(map, "$.age");
// map.size() = 1 (age field removed)
// Check if path exists
boolean exists = JSONPath.contains(map, "$.name");
// exists = true
Supported JSONPath expressions:
$ - Root object/element@ - Current object/element. or [] - Child operator.. - Recursive descent* - Wildcard[] - Array index or filter[start:end:step] - Array slice?() - Filter expression| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
JSONPath.Context
Represents the execution context for JSONPath operations
|
static class |
JSONPath.Feature
Features that can be applied to JSONPath operations
|
(package private) static class |
JSONPath.PreviousPath
Represents a previous path reference
|
(package private) static class |
JSONPath.RootPath
Represents the root path ($)
|
(package private) static class |
JSONPath.Sequence
Represents a sequence of values
|
| Modifier and Type | Field and Description |
|---|---|
(package private) long |
features
Feature flags for this JSONPath
|
(package private) static JSONReader.Context |
PARSE_CONTEXT |
(package private) java.lang.String |
path
The JSONPath expression string
|
(package private) JSONReader.Context |
readerContext
Context for JSON reading operations
|
(package private) JSONWriter.Context |
writerContext
Context for JSON writing operations
|
| Modifier | Constructor and Description |
|---|---|
protected |
JSONPath(java.lang.String path,
JSONPath.Feature... features)
Constructs a JSONPath with the specified path and features
|
protected |
JSONPath(java.lang.String path,
long features)
Constructs a JSONPath with the specified path and feature flags
|
| Modifier and Type | Method and Description |
|---|---|
void |
arrayAdd(java.lang.Object root,
java.lang.Object... values)
Adds values to an array at the specified root object
|
static JSONPath |
compile(java.lang.String path)
Deprecated.
use
of(String) instead |
static JSONPath |
compile(java.lang.String strPath,
java.lang.Class objectClass)
Compiles a JSONPath expression for a specific object class
|
abstract boolean |
contains(java.lang.Object object)
Checks if the path exists in the object
|
static boolean |
contains(java.lang.Object rootObject,
java.lang.String path)
Checks if the specified path exists in the object
|
protected JSONReader.Context |
createContext()
Creates a new reading context
|
boolean |
endsWithFilter()
Checks if this path ends with a filter
|
abstract java.lang.Object |
eval(java.lang.Object object)
Evaluates the path on the object
|
static java.lang.Object |
eval(java.lang.Object rootObject,
java.lang.String path)
Evaluates the JSONPath expression on an object
|
static java.lang.Object |
eval(java.lang.String str,
java.lang.String path)
Evaluates the JSONPath expression on a JSON string
|
java.lang.Object |
extract(byte[] jsonBytes)
Extracts a value from a JSON byte array
|
java.lang.Object |
extract(byte[] jsonBytes,
int off,
int len,
java.nio.charset.Charset charset)
Extracts a value from a JSON byte array with specified offset, length and charset
|
abstract java.lang.Object |
extract(JSONReader jsonReader)
Extracts a value using the provided JSONReader
|
void |
extract(JSONReader jsonReader,
ValueConsumer consumer)
Extracts a value using the provided JSONReader and passes it to the consumer
|
java.lang.Object |
extract(java.lang.String jsonStr)
Extracts a value from a JSON string
|
static java.lang.Object |
extract(java.lang.String json,
java.lang.String path)
Extracts a value from JSON string using the specified path
|
static java.lang.Object |
extract(java.lang.String json,
java.lang.String path,
JSONPath.Feature... features)
Extracts a value from JSON string using the specified path and features
|
java.lang.Integer |
extractInt32(JSONReader jsonReader)
Extracts an Integer value using the provided JSONReader
|
int |
extractInt32Value(JSONReader jsonReader)
Extracts an int value using the provided JSONReader
|
java.lang.Long |
extractInt64(JSONReader jsonReader)
Extracts a Long value using the provided JSONReader
|
long |
extractInt64Value(JSONReader jsonReader)
Extracts a long value using the provided JSONReader
|
abstract java.lang.String |
extractScalar(JSONReader jsonReader)
Extracts a scalar value using the provided JSONReader
|
void |
extractScalar(JSONReader jsonReader,
ValueConsumer consumer)
Extracts a scalar value using the provided JSONReader and passes it to the consumer
|
abstract JSONPath |
getParent()
Gets the parent JSONPath of this path
|
JSONReader.Context |
getReaderContext()
Gets the reading context, creating it if necessary
|
JSONWriter.Context |
getWriterContext()
Gets the writing context, creating it if necessary
|
boolean |
isPrevious()
Checks if this path represents a previous reference
|
abstract boolean |
isRef()
Checks if this path is a reference
|
(package private) static JSONPathSingle |
of(JSONPathSegment segment)
Creates a single segment JSONPath from a segment
|
static JSONPath |
of(java.lang.String path)
Creates a JSONPath from a path string
|
static JSONPath |
of(java.lang.String[] paths,
java.lang.reflect.Type[] types)
Creates a multi-path JSONPath
|
static JSONPath |
of(java.lang.String[] paths,
java.lang.reflect.Type[] types,
JSONReader.Feature... features)
Creates a multi-path JSONPath
|
static JSONPath |
of(java.lang.String[] paths,
java.lang.reflect.Type[] types,
java.lang.String[] formats,
long[] pathFeatures,
java.time.ZoneId zoneId,
JSONReader.Feature... features)
Creates a multi-path JSONPath
|
static JSONPath |
of(java.lang.String path,
JSONPath.Feature... features)
Creates a JSONPath from a path string with specified features
|
static JSONPath |
of(java.lang.String path,
java.lang.reflect.Type type)
Creates a typed JSONPath from a path string and type
|
static JSONPath |
of(java.lang.String path,
java.lang.reflect.Type type,
JSONPath.Feature... features)
Creates a typed JSONPath from a path string, type and features
|
(package private) static JSONPathFilter.Operator |
parseOperator(JSONReader jsonReader)
Parses an operator from the JSONReader
|
(package private) void |
paths(java.util.Map<java.lang.Object,java.lang.String> values,
java.util.Map paths,
java.lang.String parent,
java.lang.Object javaObject)
Recursively collects paths and values from the object
|
static java.util.Map<java.lang.String,java.lang.Object> |
paths(java.lang.Object javaObject)
Gets all paths in the object
|
abstract boolean |
remove(java.lang.Object object)
Removes a value from the object
|
static void |
remove(java.lang.Object rootObject,
java.lang.String path)
Removes a value from an object using the specified path
|
static java.lang.String |
remove(java.lang.String json,
java.lang.String path)
Removes a value from a JSON string using the specified path and returns the modified JSON
|
abstract void |
set(java.lang.Object object,
java.lang.Object value)
Sets a value in the object
|
abstract void |
set(java.lang.Object object,
java.lang.Object value,
JSONReader.Feature... readerFeatures)
Sets a value in the object with specified reader features
|
static java.lang.Object |
set(java.lang.Object rootObject,
java.lang.String path,
java.lang.Object value)
Sets a value in an object using the specified path
|
static java.lang.String |
set(java.lang.String json,
java.lang.String path,
java.lang.Object value)
Sets a value in a JSON string using the specified path and returns the modified JSON
|
abstract void |
setCallback(java.lang.Object object,
java.util.function.BiFunction callback)
Sets a callback function for the object
|
void |
setCallback(java.lang.Object object,
java.util.function.Function callback)
Sets a callback function for the object
|
static java.lang.Object |
setCallback(java.lang.Object rootObject,
java.lang.String path,
java.util.function.BiFunction callback)
Sets a callback function for the specified path in an object
|
static java.lang.Object |
setCallback(java.lang.Object rootObject,
java.lang.String path,
java.util.function.Function callback)
Sets a callback function for the specified path in an object
|
abstract void |
setInt(java.lang.Object object,
int value)
Sets an integer value in the object
|
abstract void |
setLong(java.lang.Object object,
long value)
Sets a long value in the object
|
JSONPath |
setReaderContext(JSONReader.Context context)
Sets the reading context
|
JSONPath |
setWriterContext(JSONWriter.Context writerContext)
Sets the writing context
|
java.lang.String |
toString()
Returns the string representation of this JSONPath
|
static final JSONReader.Context PARSE_CONTEXT
JSONReader.Context readerContext
JSONWriter.Context writerContext
final java.lang.String path
final long features
protected JSONPath(java.lang.String path,
JSONPath.Feature... features)
path - the JSONPath expressionfeatures - the features to applyprotected JSONPath(java.lang.String path,
long features)
path - the JSONPath expressionfeatures - the feature flags to applypublic abstract JSONPath getParent()
public boolean endsWithFilter()
public boolean isPrevious()
public final java.lang.String toString()
toString in class java.lang.Objectpublic static java.lang.Object extract(java.lang.String json,
java.lang.String path)
json - the JSON string to extract frompath - the JSONPath expressionpublic static java.lang.Object extract(java.lang.String json,
java.lang.String path,
JSONPath.Feature... features)
json - the JSON string to extract frompath - the JSONPath expressionfeatures - the features to apply during extractionpublic static java.lang.Object eval(java.lang.String str,
java.lang.String path)
str - the JSON string to evaluatepath - the JSONPath expressionpublic static java.lang.Object eval(java.lang.Object rootObject,
java.lang.String path)
rootObject - the root object to evaluatepath - the JSONPath expressionpublic static java.lang.String set(java.lang.String json,
java.lang.String path,
java.lang.Object value)
json - the JSON string to modifypath - the JSONPath expressionvalue - the value to setpublic static boolean contains(java.lang.Object rootObject,
java.lang.String path)
rootObject - the root object to checkpath - the JSONPath expressionpublic static java.lang.Object set(java.lang.Object rootObject,
java.lang.String path,
java.lang.Object value)
rootObject - the root object to modifypath - the JSONPath expressionvalue - the value to setpublic static java.lang.Object setCallback(java.lang.Object rootObject,
java.lang.String path,
java.util.function.Function callback)
rootObject - the root object to modifypath - the JSONPath expressioncallback - the callback function to setpublic static java.lang.Object setCallback(java.lang.Object rootObject,
java.lang.String path,
java.util.function.BiFunction callback)
rootObject - the root object to modifypath - the JSONPath expressioncallback - the callback function to setpublic static java.lang.String remove(java.lang.String json,
java.lang.String path)
json - the JSON string to modifypath - the JSONPath expressionpublic static void remove(java.lang.Object rootObject,
java.lang.String path)
rootObject - the root object to modifypath - the JSONPath expressionpublic static java.util.Map<java.lang.String,java.lang.Object> paths(java.lang.Object javaObject)
javaObject - the object to get paths fromvoid paths(java.util.Map<java.lang.Object,java.lang.String> values,
java.util.Map paths,
java.lang.String parent,
java.lang.Object javaObject)
values - map to store object to path mappingspaths - map to store path to value mappingsparent - the parent pathjavaObject - the object to processpublic abstract boolean isRef()
public void arrayAdd(java.lang.Object root,
java.lang.Object... values)
root - the root objectvalues - the values to addpublic abstract boolean contains(java.lang.Object object)
object - the object to checkpublic abstract java.lang.Object eval(java.lang.Object object)
object - the object to evaluateprotected JSONReader.Context createContext()
public java.lang.Object extract(java.lang.String jsonStr)
jsonStr - the JSON string to extract frompublic java.lang.Object extract(byte[] jsonBytes)
jsonBytes - the JSON bytes to extract frompublic java.lang.Object extract(byte[] jsonBytes,
int off,
int len,
java.nio.charset.Charset charset)
jsonBytes - the JSON bytes to extract fromoff - the offset in the byte arraylen - the number of bytes to readcharset - the charset to usepublic abstract java.lang.Object extract(JSONReader jsonReader)
jsonReader - the JSONReader to usepublic abstract java.lang.String extractScalar(JSONReader jsonReader)
jsonReader - the JSONReader to usepublic JSONReader.Context getReaderContext()
public JSONPath setReaderContext(JSONReader.Context context)
context - the context to setpublic JSONWriter.Context getWriterContext()
public JSONPath setWriterContext(JSONWriter.Context writerContext)
writerContext - the context to setpublic abstract void set(java.lang.Object object,
java.lang.Object value)
object - the object to modifyvalue - the value to setpublic abstract void set(java.lang.Object object,
java.lang.Object value,
JSONReader.Feature... readerFeatures)
object - the object to modifyvalue - the value to setreaderFeatures - the reader features to applypublic void setCallback(java.lang.Object object,
java.util.function.Function callback)
object - the object to modifycallback - the callback function to setpublic abstract void setCallback(java.lang.Object object,
java.util.function.BiFunction callback)
object - the object to modifycallback - the callback function to setpublic abstract void setInt(java.lang.Object object,
int value)
object - the object to modifyvalue - the integer value to setpublic abstract void setLong(java.lang.Object object,
long value)
object - the object to modifyvalue - the long value to setpublic abstract boolean remove(java.lang.Object object)
object - the object to modifypublic void extract(JSONReader jsonReader, ValueConsumer consumer)
jsonReader - the JSONReader to useconsumer - the consumer to accept the extracted valuepublic void extractScalar(JSONReader jsonReader, ValueConsumer consumer)
jsonReader - the JSONReader to useconsumer - the consumer to accept the extracted scalar valuepublic java.lang.Long extractInt64(JSONReader jsonReader)
jsonReader - the JSONReader to usepublic long extractInt64Value(JSONReader jsonReader)
jsonReader - the JSONReader to usepublic java.lang.Integer extractInt32(JSONReader jsonReader)
jsonReader - the JSONReader to usepublic int extractInt32Value(JSONReader jsonReader)
jsonReader - the JSONReader to use@Deprecated public static JSONPath compile(java.lang.String path)
of(String) insteadof(String) instead)path - the JSONPath expression to compilepublic static JSONPath compile(java.lang.String strPath, java.lang.Class objectClass)
strPath - the JSONPath expression to compileobjectClass - the class of the object to use with this pathstatic JSONPathSingle of(JSONPathSegment segment)
segment - the segment to create a path frompublic static JSONPath of(java.lang.String path)
path - the JSONPath expressionpublic static JSONPath of(java.lang.String path, java.lang.reflect.Type type)
path - the JSONPath expressiontype - the type of the resultpublic static JSONPath of(java.lang.String path, java.lang.reflect.Type type, JSONPath.Feature... features)
path - the JSONPath expressiontype - the type of the resultfeatures - the features to applypublic static JSONPath of(java.lang.String[] paths, java.lang.reflect.Type[] types)
paths - the JSONPath expressionstypes - the types of the resultspublic static JSONPath of(java.lang.String[] paths, java.lang.reflect.Type[] types, JSONReader.Feature... features)
paths - the JSONPath expressionstypes - the types of the resultsfeatures - the reader features to applypublic static JSONPath of(java.lang.String[] paths, java.lang.reflect.Type[] types, java.lang.String[] formats, long[] pathFeatures, java.time.ZoneId zoneId, JSONReader.Feature... features)
paths - the JSONPath expressionstypes - the types of the resultsformats - the formats to applypathFeatures - the path featureszoneId - the zone IDfeatures - the reader features to applypublic static JSONPath of(java.lang.String path, JSONPath.Feature... features)
path - the JSONPath expressionfeatures - the features to applystatic JSONPathFilter.Operator parseOperator(JSONReader jsonReader)
jsonReader - the JSONReader to parse from