Skip to content

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

@markmaal

Description

@markmaal

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

if r, ok := o[k]; ok {

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions