16

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" +
        "}";

3 Answers 3

24

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

3
  • 1
    I don't know why it doesn't work to me. If I inspect the dependencies I can see jsonPath correctly added to tests but I always have that Exception. Sep 8, 2017 at 8:11
  • Refreshing your project and then doing gradle refresh (on Eclipse, Right click on Project > Gradle > Refresh Gradle Project) should do it.
    – papigee
    Jun 4, 2019 at 18:57
  • I can't make the static import work of jsonPath(...). It can't find the package "matchers". I am using gradle.
    – kiltek
    Apr 16, 2020 at 7:53
21

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.

2
  • Aha, that was it! Thank you very much!
    – firechant
    Jul 18, 2013 at 15:16
  • Thank you, It is working for me after adding all dependencies. Apr 1, 2015 at 7:55
2

When there are conflicting versions of json-smart, if the older version is picked which does not have the expected class.

How to fix

Verify the versions with mvn dependency:tree.

Below, there's net.minidev:json-smart:jar:1.1.1:compile and com.jayway.jsonpath:json-path:jar:1.1.0:test which uses latest version of json-smart.

$ mvn clean dependency:tree | grep json
[INFO] +- net.logstash.log4j:jsonevent-layout:jar:1.7:compile
[INFO] |  +- net.minidev:json-smart:jar:1.1.1:compile
[INFO] |  +- io.spray:spray-json_2.11:jar:1.3.3:compile
[INFO] |  +- org.json:json:jar:20160810:compile
[INFO] +- com.github.fge:json-schema-validator:jar:2.2.6:compile
[INFO] |  +- com.github.fge:json-schema-core:jar:1.2.5:compile
[INFO] |  |  +- org.skyscreamer:jsonassert:jar:1.4.0:test
[INFO] |  |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] +- com.jayway.jsonpath:json-path:jar:1.1.0:test
[INFO] |  |  |  |  +- io.gatling:jsonpath_2.10:jar:0.6.1:test
[INFO] |  |  |  |  +- io.fastjson:boon:jar:0.28:test

So, exclude the older version of json-smart from dependency as json-path has latest version of json-smart.

eg.

    <dependency>
        <groupId>net.logstash.log4j</groupId>
        <artifactId>jsonevent-layout</artifactId>
        <version>1.7</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <groupId>net.minidev</groupId>
                <artifactId>json-smart</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.