1

I have a JSON represented in String format like shown below and i am using jsonpath.read method to find the pattern i am looking for.

    String jsonExpr="{\"store\":"
             +"{"
               +"\"book\":"
                 +"   [ "
                   +"     { \"category\": \"reference\","
                     +"     \"author\": \"Nigel Rees\","
                       +"   \"title\": \"Sayings of the Century\","
                         +" \"price\": 8.95"
                        +"},"
                        +"{ \"category\": \"fiction\","
                         +" \"author\": \"Evelyn Waugh\","
                          +"\"title\": \"Sword of Honour\","
                          +"\"price\": 12.99"
                        +"},"
                        +"{ \"category\": \"fiction\","
                         +" \"author\": \"Herman Melville\","
                          +"\"title\": \"Moby Dick\","
                          +"\"isbn\": \"0-553-21311-3\","
                          +"\"price\": 8.99"
                        +"},"
                        +"{ \"category\": \"fiction\","
                          +"\"author\": \"J. R. R. Tolkien\","
                          +"\"title\": \"The Lord of the Rings\","
                          +"\"isbn\": \"0-395-19395-8\","
                          +"\"price\": 22.99"
                        +"}"
                    +"],"
                    +"\"bicycle\":"
                    +"{"
                       +" \"color\": \"red\","
                        +"\"price\": 19.95,"
                    +"}"
                +"}"
            +"}";

And here is the pattern i am trying to find the json .

        String matchString="$.store";

            String matchValue = JsonPath.read("'"+jsonExpr+"'",matchString).toString();
                            System.out.println(" Printing matched value "+matchValue);

But i am getting exception stating that property not found in path and only $ pattern works which prints the entire JSON. Using build 1.2.0 through maven.

Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: Property ['store'] not found in path $
at com.jayway.jsonpath.internal.token.PropertyPathToken.evaluate(PropertyPathToken.java:41)
at com.jayway.jsonpath.internal.token.RootPathToken.evaluate(RootPathToken.java:53)
at com.jayway.jsonpath.internal.CompiledPath.evaluate(CompiledPath.java:53)
at com.jayway.jsonpath.internal.CompiledPath.evaluate(CompiledPath.java:61)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:179)
1
  • I also faced a same issue. I put the json in the file, and read the text file, and then used read() method and that worked :). I know I am very late, sorry for that.
    – kinshuk4
    Dec 6, 2015 at 13:14

1 Answer 1

1

Try this:

Map<String, Object> result = JsonPath.read(jsonExpr, "$.store");

Or check out the doc here.

Your Answer

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

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