Package org.ghotibeaun.json.jsonpath
Interface JSONPath
public interface JSONPath
Represents an JSONPath Expression to be evaluated.
To implement a JSONPath query, use the JSONPathFactory to create the instance, and apply the
JSONNode, a JSON String, or JSON InputStream to the expression:
JSONNode books = ParserFactory.getFactory().newParser().parse(Paths.get("/books.json"));
JSONPath jsonPath = JSONPathFactory.compile("$.store.book");
JSONArray result = jsonPath.select(book);
There are additional convenience methods to execute a JSONPath expression without first instantiating a JSONNode
instance using any of the evaluate* methods:
Path bookPath = Paths.get("/books.json");
try (InputStream bookStream = Files.newInputStream(bookPath);) {
JSONPath jsonPath = JSONPathFactory.compile("$.store.book");
JSONArray result = jsonPath.evaluate(bookStream);
} catch (IOException e) {
//handle the exception
}
- Author:
- Jim Earley
-
Method Summary
Modifier and Type Method Description JSONArrayevaluate(InputStream jsonInputStream)Parses a JSON InputStream and returns a list of JSON valuesJSONArrayevaluate(String jsonString)Parses a JSON string and returns a list of JSON values<T> TevaluateValue(InputStream jsonInputStream)Parses a JSON InputStream and returns a list of JSON values<T> TevaluateValue(String jsonString)Parses a JSON string and returns a list of JSON valuesJSONArrayselect(JSONNode context)Return a list of JSON values from a JSONNode context.<T> TselectValue(JSONNode context)Returns a single value from a JSONPath expression on a JSONNode context
-
Method Details
-
select
Return a list of JSON values from a JSONNode context.- Parameters:
context- The JSONNode to execute the JSONPath expression on- Returns:
- a JSONArray of values. Values can be any valid JSON type including JSONArray or JSONObject instances
-
selectValue
Returns a single value from a JSONPath expression on a JSONNode context- Parameters:
context- The JSONNode to execute the JSONPath expression on- Returns:
- a single value. If the JSONPath returns more than one result, then it returns only the first value; If no values are returned than it returns null; When using primitive types (e.g., String, Boolean), take care to understand the data, especially with any autoboxed values (double, boolean, int, etc.).
-
evaluate
Parses a JSON string and returns a list of JSON values- Parameters:
jsonString- the JSON string data- Returns:
- a JSONArray of values. Values can be any valid JSON type including JSONArray or JSONObject instances
-
evaluateValue
Parses a JSON string and returns a list of JSON values- Parameters:
jsonString- the JSON string data- Returns:
- a single value. If the JSONPath returns more than one result, then it returns only the first value; If no values are returned than it returns null; When using primitive types (e.g., String, Boolean), take care to understand the data, especially with any autoboxed values (double, boolean, int, etc.).
-
evaluate
Parses a JSON InputStream and returns a list of JSON values- Parameters:
jsonInputStream- the JSON InputStream- Returns:
- a JSONArray of values. Values can be any valid JSON type including JSONArray or JSONObject instances
-
evaluateValue
Parses a JSON InputStream and returns a list of JSON values- Parameters:
jsonInputStream- the JSON InputStream- Returns:
- a single value. If the JSONPath returns more than one result, then it returns only the first value; If no values are returned than it returns null; When using primitive types (e.g., String, Boolean), take care to understand the data, especially with any autoboxed values (double, boolean, int, etc.).
-