0

I am using JsonPath and am able to parse my data and get the values when the path provided is correct.

However when I provide an incorrect path (user input) the program terminates. I dont want that to happen. Is there anyway for me to catch this exceptions and continue with the next steps?

try
        {
            String value = JsonPath.read(jsonText, jsonPath);

            System.out.println(value);
        }
        catch(InvalidPathException e)
        {
            System.out.println("ERROR<InvalidPathException>: "+e.getMessage());
        }

I get following error with invalid path:

Exception in thread "main" com.jayway.jsonpath.InvalidPathException: invalid path
at com.jayway.jsonpath.internal.filter.FieldFilter.filter(FieldFilter.java:59)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:182)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:202)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:307)
at office.jsonPathparse.main(jsonPathparse.java:37)

This exception seems to be comming from FieldFilter which is jar's class.

0

2 Answers 2

2

If I understood you correctly and your catch block isn't being executed, you must be catching Java 7's java.nio.file.InvalidPathException instead of JsonPath's com.jayway.jsonpath.InvalidPathException.

Double check your import statements.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Sotirios .. I din realise I imported incorrect exception .. My code is working perfectly now.
0

Library arises invalid path exception on basis of below code(taken from grepcode)

if (jsonPath == null || jsonPath.trim().isEmpty() || jsonPath.matches("new ") 
       || jsonPath.matches("[^\\?\\+\\=\\-\\*\\/\\!]\\(")) {

       throw new InvalidPathException("Invalid path");

 }

So i think you should print jsonPath and matches it with the above condition.

Comments

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.