* JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is
* available in many programming languages such as Javascript, Python and PHP.
*
* JsonPath allows you to compile a json path string to use it many times or to compile and apply in one
* single on demand operation.
*
* Given the Json document:
*
*
* String json =
* "{
* "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
* }
* ],
* "bicycle":
* {
* "color": "red",
* "price": 19.95
* }
* }
* }";
*
*
* A JsonPath can be compiled and used as shown:
*
*
* JsonPath path = JsonPath.compile("$.store.book[1]");
*
* List<Object> books = path.read(json);
*
*
* Or:
*
*
* List<Object> authors = JsonPath.read(json, "$.store.book[*].author")
*
*
* If the json path returns a single value (is definite):
*
*
* String author = JsonPath.read(json, "$.store.book[1].author")
*
*/
public class JsonPath {
private final static Logger log = Logger.getLogger(JsonPath.class.getName());
private static JSONParser JSON_PARSER = new JSONParser();
private JsonPathFilterChain filters;
/**
* Creates a new JsonPath.
*
* @param jsonPath the path statement
*/
private JsonPath(String jsonPath) {
if (jsonPath == null ||
jsonPath.trim().isEmpty() ||
jsonPath.matches("new ") ||
jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) {
throw new InvalidPathException("Invalid path");
}
this.filters = new JsonPathFilterChain(PathUtil.splitPath(jsonPath));
}
/**
* Applies this json path to the provided object
*
* @param json a json Object
* @param
* @return list of objects matched by the given path
*/
public List read(Object json) {
return (List) filters.filter(json);
}
/**
* Applies this json path to the provided object
*
* @param json a json string
* @param
* @return list of objects matched by the given path
*/
public List read(String json) throws java.text.ParseException {
return read(parse(json));
}
/**
* Compiles a JsonPath from the given string
*
* @param jsonPath to compile
* @return compiled JsonPath
*/
public static JsonPath compile(String jsonPath) {
return new JsonPath(jsonPath);
}
/**
* Creates a new JsonPath and applies it to the provided Json string
*
* @param json a json string
* @param jsonPath the json path
* @param
* @return list of objects matched by the given path
*/
public static List read(String json, String jsonPath) throws java.text.ParseException {
return compile(jsonPath).read(json);
}
/**
* Creates a new JsonPath and applies it to the provided Json object
*
* @param json a json object
* @param jsonPath the json path
* @param
* @return list of objects matched by the given path
*/
public static List read(Object json, String jsonPath) {
return compile(jsonPath).read(json);
}
/**
* Creates a new JsonPath and applies it to the provided Json object. Note this method
* will throw an exception if the provided path returns more than one object. This method
* can be used with paths that are not definite but a warning will be generated.
*
* @param json a json object
* @param jsonPath the json path
* @param
* @return the object matched by the given path
*/
public static T readOne(Object json, String jsonPath) {
List