PaesslerAG / jsonpath

BSD 3-Clause "New" or "Revised" License
172 stars 37 forks source link

Invalid/undefined keys error out in JSONPath, but not in GVal #41

Open markmaal opened 6 months ago

markmaal commented 6 months ago

Hey,

Is there a reason why GVal defaults to returning nil if the expression references an undefined data key, but JsonPath errors out?

https://github.com/PaesslerAG/gval/blob/cd026a3dee26df39acddc2292229512c4fa68756/evaluable.go#L133 vs https://github.com/PaesslerAG/jsonpath/blob/3484786d6f977735413b0c538c298df9a9f7a5e2/selector.go#L92

As an example,

    g := gval.NewLanguage(
        gval.Full(),
        jsonpath.Language())
    value, err := g.Evaluate(`$.data`, map[string]any{"data": "test"})
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(value)

Results in test; however, if the expression is $.invalidData then we error out: can not evaluate $.invalidData: unknown key invalidData.

BUT if we just use standard Gval expressions, i.e:

    value, err := g.Evaluate(`invalidData`, map[string]any{"data": "test"})

we get nil as a return value.

The only reason I can think of is to have a distinction between null values and undefined values in JSON (null -> nil, undefined -> error), but we don't always want to error out if the key is undefined, it'd be nice if we could configure this default behavior. I might be missing something here so please let me know if this is the case!