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" +
"}";
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.
use the Deserialize activity (Deserialize Json) and you can check any element in the JSON or do any operations on it, as needed.
Represents the agent environment of the current program, if an agent is running it. DocumentContext (AgentContext - Java™) Read-only. The in-memory document when an agent starts.
You can open up a window to evaluate JSONPath expressions by going to Edit -> Find -> "Evaluate JSONPath Expression...". If a JSON file is open, it will use this file to evaluate the expression. If you have JSONPath expressions as Strings in code, use "inject language" and say this is a JSONPath expression.
If You are using Maven or Gradle, just need to add this to your dependencies list:
For Maven projects:
In your Maven pom.xml file :
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
For Gradle projects:
In the dependencies section in your Gradle build.gradle file
testCompile 'com.jayway.jsonpath:json-path:2.2.0'
It should work
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With