7

I want to get the value from the JSON object using JsonPath.Could anyone please suggest me the appropriate jars which i would need because as per my knowledge i am getting this exception for the jars i am using for jsonpath .

package jsonPg;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.jayway.jsonpath.JsonPath;

public class ReadJsonPath {

    static String file = "D:\\AutomationSample\\Sample_Json.txt";

    public static void main(String[] args) throws JSONException, IOException {
        JsonReadFile jsonReadFile=new JsonReadFile();
        JSONObject jsonObj=jsonReadFile.parseJSONFile(file);
        String jsonObject=jsonObj.toString();
        String json="";
        System.out.println(jsonObject);
//      Object val = JsonPath.read(jsonObject,"");
        String val1=JsonPath.read(jsonObject," $.payload[*].supplierDataMap[*].COMPANYDETAILS.customFieldList[*].DISPLAYGSID   .value");
        System.out.println(val1);

     }

 }

here is the code which i have written and below is the exception which is thrown at runtime

    Exception in thread "main" java.lang.NoSuchFieldError: FACTORY_SIMPLE
     at com.jayway.jsonpath.spi.impl.JsonSmartJsonProvider.<init>(JsonSmartJsonProvider.java:38)
at com.jayway.jsonpath.spi.impl.JsonSmartJsonProvider.<init>(JsonSmartJsonProvider.java:41)
at com.jayway.jsonpath.spi.JsonProviderFactory.<clinit>  (JsonProviderFactory.java:24)
at    com.jayway.jsonpath.Configuration.defaultConfiguration(Configuration.java:62)
at com.jayway.jsonpath.internal.JsonReader.<init>(JsonReader.java:26)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:462)
at jsonPg.ReadJsonPath.main(ReadJsonPath.java:27)`

Any kind of help would be appreciated . Thanks in advance .

3
  • Did U checked your path? That can be done using one of many online tools, e.g. jsonpath.com. Commented May 5, 2017 at 9:27
  • yes i have checked the jsonPath . it is working properly.. i just need to make that work in java code Commented May 5, 2017 at 10:01
  • Could you maybe share json? Commented May 5, 2017 at 10:18

2 Answers 2

6

You can achieve your goal with JsonPath library on its own. Here is an example:

    String jsonString = "{ \"list\": [ { \"name\": \"foo1\"}, { \"name\": \"foo2\"} ]}";
    DocumentContext docCtx = JsonPath.parse(jsonString);
    JsonPath jsonPath = JsonPath.compile("$.list[?(@.name == \"foo1\")]");
    JSONArray val1=docCtx.read(jsonPath);
    System.out.println(val1);

This code will print out:

[{"name":"foo1"}]

Required maven dependency:

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.2.0</version>
</dependency>

json-path will also automatically pull json-smart JAR:

<dependency>
    <groupId>net.minidev</groupId>
    <artifactId>json-smart</artifactId>
    <version>2.2.1</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

0
   String jsonString = "{ \"list\": [ { \"name\": \"foo1\"}, { \"name\": \"foo2\"} ]}";
    DocumentContext docCtx = JsonPath.parse(jsonString);
    JsonPath jsonPath = JsonPath.compile("$.list[?(@.name == foo1)]");
    JSONArray val1=docCtx.read(jsonPath);
    System.out.println(val1);

2 Comments

What's the difference with @RZet's prior and more detailed answer?
This answer is more suitable for people who loves puzzles. Perhaps Sherlock Holmes would prefer this answer. :-P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.