Closed kabukky closed 5 years ago
No problem.
Jsonpath itself doesn't contain a script language. The examples from goessner use java script (@.length-1)] and (@.price<10).
This lib does not include javascribt but it is build in a way that it can be extended. So you can include a scripting language. Take a look at the gval example. It includes the expression language gval https://github.com/PaesslerAG/gval and is able to evaluate minus and smaller than operators.
I should probably include this to the package description.
Thanks! That makes sense.
I'm creating the language using gval.Full(...)
now, which works great:
expression := "$..book[?(@.price<10)]"
jsonPathLanguage = gval.Full(jsonpath.Language(), gvalstrings.SingleQuoted())
...
path, err := jsonPathLanguage.NewEvaluable(expression)
...
result, err := path(context.Background(), v)
I realize this questions is probably better suited for gval, but:
Is there a way with gval to simply test for the existence of a field? The expression $..book[?(@.isbn)]
will return books where the isbn
field is a boolean with value true
.
I haven't implemented a === or a nil / null constant. If you're interessted feel free to implement it 😃
A json object is represented by a map[string]interface{} so $..book[?("isbn" in @)]
.
In this case $..book[?(@.isbn != "")]
should also do the job.
I'll try that! Thanks for putting up with me :)
Hi, sorry for opening another issue so soon after the first one. I took a few examples off https://goessner.net/articles/JsonPath/index.html#e3 but can't get them to work.
Example code:
Will print an empty result.
Other results using the example expressions:
$..book[-1:]
works$..book[(@.length-1)]
leads toparsing error: $..book[(@.length-1)] :1:18 - 1:19 unexpected "-" while scanning parentheses expected ")"
$..book[?(@.price<10)]
leads toparsing error: $..book[?(@.price<10)] :1:18 - 1:19 unexpected "<" while scanning parentheses expected ")"
1 suggests that expressions containing
[?(@.foo)]
should work, but I can't reproduce that.Does your syntax differ from https://goessner.net/articles/JsonPath/ ?