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

Some methods (i.e. DocumentContext's set) fail when JSON name has ' in it #171

Closed
oleg-gr opened this issue Dec 14, 2015 · 4 comments
Closed

Comments

@oleg-gr
Copy link

oleg-gr commented Dec 14, 2015

Given this JSON object

{
  "can delete": "this",
  "can't delete": "this"
}

Trying to run

Configuration configuration = Configuration.defaultConfiguration().jsonProvider(new JacksonJsonNodeJsonProvider()).mappingProvider(new JacksonMappingProvider()).addOptions(Option.AS_PATH_LIST);
DocumentContext context = JsonPath.parse(JSON_NODE, configuration);
context.set("$.['can delete']", null);
context.set("$.['can't delete']", null); # this will fail

This is a simplified example but ' is valid in the name according to JSON standard. More over, running

context.read("$..*")

will return

["$.['can delete']", "$.['can't delete']"]

and will cause issues, when trying to use the second value, e.g. context.set(paths[1], null); where paths is an array of paths gotten from context.read("$..*")

@oleg-gr oleg-gr changed the title Some methods (i.e. DocumentContext's set) fail when JSON name has Some methods (i.e. DocumentContext's set) fail when JSON name has ' in it Dec 14, 2015
@kallestenflo
Copy link
Contributor

This is fixed but you must escape the single quote in the path:

@Test
public void issue_171() {

    String json = "{\n" +
            "  \"can delete\": \"this\",\n" +
            "  \"can't delete\": \"this\"\n" +
            "}";

    DocumentContext context = using(JACKSON_JSON_NODE_CONFIGURATION).parse(json);
    context.set("$.['can delete']", null);
    context.set("$.['can\\'t delete']", null);

    ObjectNode objectNode = context.read("$");

    assertThat(objectNode.get("can delete").isNull());
    assertThat(objectNode.get("can't delete").isNull());
}

@oleg-gr
Copy link
Author

oleg-gr commented Dec 15, 2015

@kallestenflo thank you. Any plan to change paths returned by context.read("$..*") containing '? If I am going to use a path returned by context.read containing "$.['can't delete']" I will have to manually escape the ' in the middle only

@oleg-gr
Copy link
Author

oleg-gr commented Dec 17, 2015

@kallestenflo any response to that? Since I am reusing results from things like context.read("$..*") it would be nice to get the paths already escaped (maybe to have it as an option) so that there aren't a hundred places where it needs to be done manually

@oleg-gr
Copy link
Author

oleg-gr commented Dec 17, 2015

By the way, context.set("$.['can\\\\'t delete']", null); will fail with stack overflow. Can you maybe detect it and throw some error, rather than overflowing the stack?

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

2 participants