Is it possible to use JSONPath to pick the objects without a particular field?
denisvmedia opened this issue · 4 comments
denisvmedia commented
I tried this program: https://go.dev/play/p/De9ncvFbK6S:
package main
import (
"fmt"
"github.com/ohler55/ojg/jp"
"github.com/ohler55/ojg/oj"
)
func main() {
obj, err := oj.ParseString(`[{"name": "Alice", "age": 20}, {"age": 30}, {"name": "Bob"}]`)
if err != nil {
panic(err)
}
// Define the JSONPath expression to select objects without a "name" field.
x, err := jp.ParseString("[?(!@.name)]")
if err != nil {
panic(err)
}
ys := x.Get(obj)
fmt.Println(ys)
}
It won't panic, but it also just returns all the objects regardless the filter. Is it possible to achieve what I need with your library? Thank you.
ohler55 commented
[?(@.name exists false)]
should work but I tried it an it fails so it's a bug and I'll fix it tonight.
ohler55 commented
Looks like I broke it in the last release when the Nothing
value was introduced to be in close compliance with https://datatracker.ietf.org/doc/html/draft-ietf-jsonpath-base. Please try the develop branch for the fix. I'll release on confirmation.
denisvmedia commented
It works. Thank you.
ohler55 commented
Released v1.18.1 with the fix. Thanks for letting me know about the issue.