Package com.alibaba.fastjson2
Class JSONPath
java.lang.Object
com.alibaba.fastjson2.JSONPath
- Direct Known Subclasses:
JSONPathCompilerReflect.SingleNamePathTyped,JSONPathCompilerReflect.TwoNameSegmentTypedPath,JSONPathTypedMultiNamesPrefixIndex1,JSONPathTypedMultiNamesPrefixName1,JSONPathTypedMultiNamesPrefixName2
Represents a JSONPath expression for querying and manipulating JSON data.
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
- Since:
- 2.0.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumFeatures that can be applied to JSONPath operations -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs a JSONPath with the specified path and feature flagsprotectedJSONPath(String path, JSONPath.Feature... features) Constructs a JSONPath with the specified path and features -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds values to an array at the specified root objectstatic JSONPathDeprecated.static JSONPathCompiles a JSONPath expression for a specific object classabstract booleanChecks if the path exists in the objectstatic booleanChecks if the specified path exists in the objectprotected JSONReader.ContextCreates a new reading contextbooleanChecks if this path ends with a filterabstract ObjectEvaluates the path on the objectstatic ObjectEvaluates the JSONPath expression on an objectstatic ObjectEvaluates the JSONPath expression on a JSON stringextract(byte[] jsonBytes) Extracts a value from a JSON byte arrayExtracts a value from a JSON byte array with specified offset, length and charsetabstract Objectextract(JSONReader jsonReader) Extracts a value using the provided JSONReadervoidextract(JSONReader jsonReader, ValueConsumer consumer) Extracts a value using the provided JSONReader and passes it to the consumerExtracts a value from a JSON stringstatic ObjectExtracts a value from JSON string using the specified pathstatic Objectextract(String json, String path, JSONPath.Feature... features) Extracts a value from JSON string using the specified path and featuresextractInt32(JSONReader jsonReader) Extracts an Integer value using the provided JSONReaderintextractInt32Value(JSONReader jsonReader) Extracts an int value using the provided JSONReaderextractInt64(JSONReader jsonReader) Extracts a Long value using the provided JSONReaderlongextractInt64Value(JSONReader jsonReader) Extracts a long value using the provided JSONReaderabstract StringextractScalar(JSONReader jsonReader) Extracts a scalar value using the provided JSONReadervoidextractScalar(JSONReader jsonReader, ValueConsumer consumer) Extracts a scalar value using the provided JSONReader and passes it to the consumerabstract JSONPathGets the parent JSONPath of this pathGets the reading context, creating it if necessaryGets the writing context, creating it if necessarybooleanChecks if this path represents a previous referenceabstract booleanisRef()Checks if this path is a referencestatic JSONPathCreates a JSONPath from a path stringstatic JSONPathCreates a multi-path JSONPathstatic JSONPathof(String[] paths, Type[] types, JSONReader.Feature... features) Creates a multi-path JSONPathstatic JSONPathof(String[] paths, Type[] types, String[] formats, long[] pathFeatures, ZoneId zoneId, JSONReader.Feature... features) Creates a multi-path JSONPathstatic JSONPathof(String path, JSONPath.Feature... features) Creates a JSONPath from a path string with specified featuresstatic JSONPathCreates a typed JSONPath from a path string and typestatic JSONPathof(String path, Type type, JSONPath.Feature... features) Creates a typed JSONPath from a path string, type and featuresGets all paths in the objectabstract booleanRemoves a value from the objectstatic voidRemoves a value from an object using the specified pathstatic StringRemoves a value from a JSON string using the specified path and returns the modified JSONabstract voidSets a value in the objectabstract voidset(Object object, Object value, JSONReader.Feature... readerFeatures) Sets a value in the object with specified reader featuresstatic ObjectSets a value in an object using the specified pathstatic StringSets a value in a JSON string using the specified path and returns the modified JSONstatic ObjectsetCallback(Object rootObject, String path, BiFunction callback) Sets a callback function for the specified path in an objectstatic ObjectsetCallback(Object rootObject, String path, Function callback) Sets a callback function for the specified path in an objectabstract voidsetCallback(Object object, BiFunction callback) Sets a callback function for the objectvoidsetCallback(Object object, Function callback) Sets a callback function for the objectabstract voidSets an integer value in the objectabstract voidSets a long value in the objectsetReaderContext(JSONReader.Context context) Sets the reading contextsetWriterContext(JSONWriter.Context writerContext) Sets the writing contextfinal StringtoString()Returns the string representation of this JSONPath
-
Constructor Details
-
JSONPath
Constructs a JSONPath with the specified path and features- Parameters:
path- the JSONPath expressionfeatures- the features to apply
-
JSONPath
Constructs a JSONPath with the specified path and feature flags- Parameters:
path- the JSONPath expressionfeatures- the feature flags to apply
-
-
Method Details
-
getParent
Gets the parent JSONPath of this path- Returns:
- the parent JSONPath, or null if this is the root path
-
endsWithFilter
public boolean endsWithFilter()Checks if this path ends with a filter- Returns:
- true if this path ends with a filter, false otherwise
-
isPrevious
public boolean isPrevious()Checks if this path represents a previous reference- Returns:
- true if this path is a previous reference, false otherwise
-
toString
Returns the string representation of this JSONPath -
extract
Extracts a value from JSON string using the specified path- Parameters:
json- the JSON string to extract frompath- the JSONPath expression- Returns:
- the extracted value, or null if not found
-
extract
Extracts a value from JSON string using the specified path and features- Parameters:
json- the JSON string to extract frompath- the JSONPath expressionfeatures- the features to apply during extraction- Returns:
- the extracted value, or null if not found
-
eval
Evaluates the JSONPath expression on a JSON string- Parameters:
str- the JSON string to evaluatepath- the JSONPath expression- Returns:
- the evaluated result, or null if not found
-
eval
Evaluates the JSONPath expression on an object- Parameters:
rootObject- the root object to evaluatepath- the JSONPath expression- Returns:
- the evaluated result, or null if not found
-
set
Sets a value in a JSON string using the specified path and returns the modified JSON- Parameters:
json- the JSON string to modifypath- the JSONPath expressionvalue- the value to set- Returns:
- the modified JSON string
-
contains
Checks if the specified path exists in the object- Parameters:
rootObject- the root object to checkpath- the JSONPath expression- Returns:
- true if the path exists, false otherwise
-
set
Sets a value in an object using the specified path- Parameters:
rootObject- the root object to modifypath- the JSONPath expressionvalue- the value to set- Returns:
- the modified root object
-
setCallback
Sets a callback function for the specified path in an object- Parameters:
rootObject- the root object to modifypath- the JSONPath expressioncallback- the callback function to set- Returns:
- the modified root object
-
setCallback
Sets a callback function for the specified path in an object- Parameters:
rootObject- the root object to modifypath- the JSONPath expressioncallback- the callback function to set- Returns:
- the modified root object
-
remove
Removes a value from a JSON string using the specified path and returns the modified JSON- Parameters:
json- the JSON string to modifypath- the JSONPath expression- Returns:
- the modified JSON string
-
remove
Removes a value from an object using the specified path- Parameters:
rootObject- the root object to modifypath- the JSONPath expression
-
paths
Gets all paths in the object- Parameters:
javaObject- the object to get paths from- Returns:
- a map of paths to values
-
isRef
public abstract boolean isRef()Checks if this path is a reference- Returns:
- true if this path is a reference, false otherwise
-
arrayAdd
Adds values to an array at the specified root object- Parameters:
root- the root objectvalues- the values to add
-
contains
Checks if the path exists in the object- Parameters:
object- the object to check- Returns:
- true if the path exists, false otherwise
-
eval
Evaluates the path on the object- Parameters:
object- the object to evaluate- Returns:
- the evaluation result
-
createContext
Creates a new reading context- Returns:
- a new JSONReader.Context
-
extract
Extracts a value from a JSON string- Parameters:
jsonStr- the JSON string to extract from- Returns:
- the extracted value, or null if input is null
-
extract
Extracts a value from a JSON byte array- Parameters:
jsonBytes- the JSON bytes to extract from- Returns:
- the extracted value, or null if input is null
-
extract
Extracts a value from a JSON byte array with specified offset, length and charset- Parameters:
jsonBytes- the JSON bytes to extract fromoff- the offset in the byte arraylen- the number of bytes to readcharset- the charset to use- Returns:
- the extracted value, or null if input is null
-
extract
Extracts a value using the provided JSONReader- Parameters:
jsonReader- the JSONReader to use- Returns:
- the extracted value
-
extractScalar
Extracts a scalar value using the provided JSONReader- Parameters:
jsonReader- the JSONReader to use- Returns:
- the extracted scalar value
-
getReaderContext
Gets the reading context, creating it if necessary- Returns:
- the JSONReader.Context
-
setReaderContext
Sets the reading context- Parameters:
context- the context to set- Returns:
- this JSONPath instance
-
getWriterContext
Gets the writing context, creating it if necessary- Returns:
- the JSONWriter.Context
-
setWriterContext
Sets the writing context- Parameters:
writerContext- the context to set- Returns:
- this JSONPath instance
-
set
Sets a value in the object- Parameters:
object- the object to modifyvalue- the value to set
-
set
Sets a value in the object with specified reader features- Parameters:
object- the object to modifyvalue- the value to setreaderFeatures- the reader features to apply
-
setCallback
Sets a callback function for the object- Parameters:
object- the object to modifycallback- the callback function to set
-
setCallback
Sets a callback function for the object- Parameters:
object- the object to modifycallback- the callback function to set
-
setInt
Sets an integer value in the object- Parameters:
object- the object to modifyvalue- the integer value to set
-
setLong
Sets a long value in the object- Parameters:
object- the object to modifyvalue- the long value to set
-
remove
Removes a value from the object- Parameters:
object- the object to modify- Returns:
- true if removal was successful, false otherwise
-
extract
Extracts a value using the provided JSONReader and passes it to the consumer- Parameters:
jsonReader- the JSONReader to useconsumer- the consumer to accept the extracted value
-
extractScalar
Extracts a scalar value using the provided JSONReader and passes it to the consumer- Parameters:
jsonReader- the JSONReader to useconsumer- the consumer to accept the extracted scalar value
-
extractInt64
Extracts a Long value using the provided JSONReader- Parameters:
jsonReader- the JSONReader to use- Returns:
- the extracted Long value, or null if the value was null
-
extractInt64Value
Extracts a long value using the provided JSONReader- Parameters:
jsonReader- the JSONReader to use- Returns:
- the extracted long value, or 0 if the value was null
-
extractInt32
Extracts an Integer value using the provided JSONReader- Parameters:
jsonReader- the JSONReader to use- Returns:
- the extracted Integer value, or null if the value was null
-
extractInt32Value
Extracts an int value using the provided JSONReader- Parameters:
jsonReader- the JSONReader to use- Returns:
- the extracted int value, or 0 if the value was null
-
compile
Deprecated.useof(String)insteadCompiles a JSONPath expression (deprecated, useof(String)instead)- Parameters:
path- the JSONPath expression to compile- Returns:
- the compiled JSONPath
-
compile
Compiles a JSONPath expression for a specific object class- Parameters:
strPath- the JSONPath expression to compileobjectClass- the class of the object to use with this path- Returns:
- the compiled JSONPath
-
of
Creates a JSONPath from a path string- Parameters:
path- the JSONPath expression- Returns:
- the created JSONPath
-
of
Creates a typed JSONPath from a path string and type- Parameters:
path- the JSONPath expressiontype- the type of the result- Returns:
- the created JSONPath
-
of
Creates a typed JSONPath from a path string, type and features- Parameters:
path- the JSONPath expressiontype- the type of the resultfeatures- the features to apply- Returns:
- the created JSONPath
-
of
Creates a multi-path JSONPath- Parameters:
paths- the JSONPath expressionstypes- the types of the results- Returns:
- the created JSONPath
- Since:
- 2.0.20
-
of
Creates a multi-path JSONPath- Parameters:
paths- the JSONPath expressionstypes- the types of the resultsfeatures- the reader features to apply- Returns:
- the created JSONPath
- Since:
- 2.0.20
-
of
public static JSONPath of(String[] paths, Type[] types, String[] formats, long[] pathFeatures, ZoneId zoneId, JSONReader.Feature... features) Creates a multi-path JSONPath- Parameters:
paths- the JSONPath expressionstypes- the types of the resultsformats- the formats to applypathFeatures- the path featureszoneId- the zone IDfeatures- the reader features to apply- Returns:
- the created JSONPath
- Since:
- 2.0.20
-
of
Creates a JSONPath from a path string with specified features- Parameters:
path- the JSONPath expressionfeatures- the features to apply- Returns:
- the created JSONPath
-
of(String)instead