JsonPath NoClassDefFoundError, or an alternative to JsonPath in Java
Asked Answered
I

3

17

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" +
        "}";
Inez answered 18/7, 2013 at 14:26 Comment(0)
B
20

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.

Blackfoot answered 18/7, 2013 at 15:6 Comment(2)
Aha, that was it! Thank you very much!Inez
Thank you, It is working for me after adding all dependencies.Parts
O
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

Openhearted answered 10/5, 2016 at 15:40 Comment(3)
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.Autogamy
Refreshing your project and then doing gradle refresh (on Eclipse, Right click on Project > Gradle > Refresh Gradle Project) should do it.Hardigg
I can't make the static import work of jsonPath(...). It can't find the package "matchers". I am using gradle.Naman
B
20

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.

Blackfoot answered 18/7, 2013 at 15:6 Comment(2)
Aha, that was it! Thank you very much!Inez
Thank you, It is working for me after adding all dependencies.Parts
K
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>
Keenan answered 7/5, 2017 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.