Open
Description
JsonPath: 2.9.0
Json smart: 2.5.2
asm: 1.0.2
Java: 17
Expression $['items'][(@.length-1)]['items'][0]
worked fine with 2.0.0, but throws following exception after upgrade to 2.9.0:
Exception in thread "main" com.jayway.jsonpath.InvalidPathException: Could not parse token starting at position 10. Expected ?, ', 0-9, *
at com.jayway.jsonpath.internal.path.PathCompiler.fail(PathCompiler.java:642)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:139)
at com.jayway.jsonpath.internal.path.PathCompiler.readBracketPropertyToken(PathCompiler.java:638)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:137)
at com.jayway.jsonpath.internal.path.PathCompiler.readDotToken(PathCompiler.java:175)
at com.jayway.jsonpath.internal.path.PathCompiler.readNextToken(PathCompiler.java:143)
at com.jayway.jsonpath.internal.path.PathCompiler.readContextToken(PathCompiler.java:123)
at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:58)
at com.jayway.jsonpath.internal.path.PathCompiler.compile(PathCompiler.java:75)
at com.jayway.jsonpath.JsonPath.<init>(JsonPath.java:97)
at com.jayway.jsonpath.JsonPath.compile(JsonPath.java:515)
Code:
String jsonString = .. // from json file
Configuration cfg = Configuration.builder().options(Option.ALWAYS_RETURN_LIST) .build();
ReadContext jsonReadContext = JsonPath.using(cfg).parse(jsonString);
String jsonPath = "$['items'][(@.length-1)]['items'][0]";
JsonPath jsonPathQueryCompiled = JsonPath.compile(jsonPath);
Object value = jsonReadContext.read(jsonPathQueryCompiled);
System.out.println(value);
Sample json data to reproduce:
{
"items": [{
"id": "test-a",
"items": [{
"id": "result-a",
"value": "276"
},
{
"id": "result-b",
"value": "0001"
}]
},
{
"id": "test-b",
"items": [{
"id": "result-a",
"value": "34"
},
{
"id": "result-b",
"value": "0001"
}]
}]
}
Expected result: [{"id":"result-a","value":"34"}]
I understand modifying json path to $['items'][-1]['items'][0]
gives the desired result. But we don't want to bother clients to modify their json paths. What options do I have? Can updating configuration in any way help?
Are there any other compatibility issues between 2.0.0 and 2.9.0?
Any help would be greatly appreciated. Thanks
Metadata
Metadata
Assignees
Labels
No labels