Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JsonPath NoClassDefFoundError, or an alternative to JsonPath in Java

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" +
        "}";
like image 451
firechant Avatar asked Jul 18 '13 14:07

firechant


People also ask

What is JSONPath in Java?

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.

How do I check if JSON path exists in JSON?

use the Deserialize activity (Deserialize Json) and you can check any element in the JSON or do any operations on it, as needed.

What is DocumentContext in Java?

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.

How do you evaluate a JSON path?

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.


2 Answers

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

like image 88
JorelC Avatar answered Oct 03 '22 20:10

JorelC


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.

like image 41
Fedy2 Avatar answered Oct 03 '22 20:10

Fedy2