Support script expressions
Closed this issue · 6 comments
The current JsonPath spec allows script expressions aka functions, are there any plans to support it?
$.books.size()
$.somestring.split(".")[0]
Which JsonPath spec? Can you provide a sample json and the expected result of your example?
http://goessner.net/articles/JsonPath/
"Script expression evaluated by the underlying script engine"
For example the jayway implementation allows some functions, min, max, etc.
https://github.com/json-path/JsonPath#functions
Ideally this would need to be an addition to the antlr grammar to allow pluggable functions that could be defined in the JsonSurfer instance
{
"items": [1,2,3,-1]
}
$.items.size() // 4
$.items.min() // -1
$.items.toString() // "[1,2,3,-1]"
I think it would not be too difficult if the "functions" apply only to leaf level(as the last node), then the implementation should be similar to JsonPath filter. Any pull requests for this feature are welcome. I'd love to help, if you have any further question.
Alternatively, you can use stream API of Java8 to achieve above "functions". Please refer to https://github.com/jsurfer/JsonSurfer#stream-support
Examples added in document:
https://github.com/jsurfer/JsonSurfer#java-8-streams-api-support