ohler55/ojg

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

denisvmedia opened this issue · 4 comments

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.

[?(@.name exists false)] should work but I tried it an it fails so it's a bug and I'll fix it tonight.

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.

It works. Thank you.

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