Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renameKey throws PathNotFoundException even if SUPPRESS_EXCEPTIONS is set #537

Closed
demonbug opened this issue Apr 11, 2019 · 4 comments
Closed

Comments

@demonbug
Copy link

demonbug commented Apr 11, 2019

version 2.3.0

Configuration jsonConf = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);

JsonPath.using(jsonConf).parse("{}").read("missing");
JsonPath.using(jsonConf)
.parse("{"list":[{"data":{"old":1}},{"data":{}},{"data":{"old":2}}]}")
.renameKey("$..data", "old", "new");

read will not throw PathNotFoundException but renameKey will.
in this case, old:1 will be renamed, but old:2 will not

@lukegil
Copy link

lukegil commented Jul 16, 2020

This also appears to be the case for delete.

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;

public class Tester {
  public static void main(String[] args) {
    String jsonString = "{\"top\": {\"middle\": null}}";
    JsonPath path = JsonPath.compile("$.top.middle.bottom");

    JsonPath.using(Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS))
        .parse(jsonString)
        .delete(path);
  }
}

Error:

Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['bottom'] in path $['top']['middle'] but found 'null'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
	at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:71)
	at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
	at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
	at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
	at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
	at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
	at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
	at com.jayway.jsonpath.JsonPath.delete(JsonPath.java:262)
	at com.jayway.jsonpath.internal.JsonContext.delete(JsonContext.java:172)
	at com.....Tester.main(Tester.java:14)

Process finished with exit code 1

System info:

openjdk version "1.8.0_222"
OpenJDK Runtime Environment Corretto-8.222.10.1 (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM Corretto-8.222.10.1 (build 25.222-b10, mixed mode)

Relevant mvn dependency:tree output:

+- com.jayway.jsonpath:json-path:jar:2.4.0:compile
 \- net.minidev:json-smart:jar:2.3:compile
   \- net.minidev:accessors-smart:jar:1.2:compile
     \- org.ow2.asm:asm:jar:5.0.4:compile

@hezonghan
Copy link
Contributor

Hello! I would try to help with this issue.

@hezonghan
Copy link
Contributor

Since I cannot find a good way to fix the problem related to delete method, I would open a new issue #721 for it.
I will give a solution there, which just shuts up the exception but not silently continue along an array.

@kallestenflo
Copy link
Contributor

Fixed by #719

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants