ohler55 / ojg

Optimized JSON for Go
MIT License
839 stars 50 forks source link

Is it possible to use JSONPath to pick the objects without a particular field? #121

Closed denisvmedia closed 1 year ago

denisvmedia commented 1 year ago

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 1 year ago

[?(@.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 1 year ago

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 1 year ago

It works. Thank you.

ohler55 commented 1 year ago

Released v1.18.1 with the fix. Thanks for letting me know about the issue.