For reasons related to the project I'm working on, I'd like an entire query to a JSON file to be held as a string, for example, $.store.book[*].title
(rather than having to store each level of the document temporarily as a separate object).
I'm currently using JsonPath (version 0.8.0, which was the latest I could find), which is basically exactly what I'm looking for, but I'm getting the exception shown below. I'm just using the sample JSON given on the JsonPath google code page, using one of their sample queries.
What am I doing wrong here? Alternatively, if there's not a solution, are there alternatives to JsonPath in Java? I want to be able to pass an entire query as a string, and it must be in Java.
The function:
public void testJsonPath() throws Exception
{
String query = "$.store.book[*].title";
List toRet = (List) JsonPath.read(practiceJson, query, new Filter[0]);
System.out.println(toRet.toString());
}
The exception:
java.lang.NoClassDefFoundError: net/minidev/json/parser/ParseException
at com.jayway.jsonpath.spi.JsonProviderFactory$1.create(JsonProviderFactory.java:27)
at com.jayway.jsonpath.spi.JsonProviderFactory.createProvider(JsonProviderFactory.java:32)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:202)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:307)
at net.windward.datasource.test.TestJsonDataSource.testJsonPath(TestJsonDataSource.java:119)
The practice JSON:
private String practiceJson = "{\n" +
" \"store\": {\n" +
" \"book\": [ {\n" +
" \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" }, {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99\n" +
" }, {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"Herman Melville\",\n" +
" \"title\": \"Moby Dick\",\n" +
" \"isbn\": \"0-553-21311-3\",\n" +
" \"price\": 8.99\n" +
" }, {\n" +
" \"category\": \"fiction\",\n" +
" \"author\": \"J. R. R. Tolkien\",\n" +
" \"title\": \"The Lord of the Rings\",\n" +
" \"isbn\": \"0-395-19395-8\",\n" +
" \"price\": 22.99\n" +
" } ],\n" +
" \"bicycle\": [ {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95,\n" +
" \"style\": [ \"city\", \"hybrid\" ]\n" +
" }, {\n" +
" \"color\": \"blue\",\n" +
" \"price\": 59.91,\n" +
" \"style\": [ \"downhill\", \"freeride\" ]\n" +
" } ]\n" +
" }\n" +
"}";
I think you have to add these dependencies to your project: https://code.google.com/p/json-path/downloads/detail?name=json-path-0.8.0-dependencies.zip&can=2&q=
In particular json-smart-1.1.jar where the missing Exception class is contained.