We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$ cat x.json {"a": 123} $ ajson 'is_null($.b)' x.json null $ ajson '$.b == null' x.json [] $ ajson '$' x.json [{"a": 123}]
the go code works the same way, if the given key doesn't exist, its value is not null, and is_null() check is false.
null
is_null()
so is there any way to check this? or a new function helper is needed?
The text was updated successfully, but these errors were encountered:
Hi! Right now there is no easy way how to do it, this possibility will be unblocked with the function of many arguments.
I can suggest you to use this way: https://play.golang.com/p/HRFPdZPCxB-
package main import ( "fmt" "github.com/spyzhov/ajson" ) func main() { json := []byte(example) root := ajson.Must(ajson.Unmarshal(json)) exists := must(root.JSONPath(`$.store.book[?(@.price || @.price == false || @.price == 0 || is_null(@.price))]`)) NOT_exists := must(root.JSONPath(`$.store.book[?(not(@.price || @.price == false || @.price == 0 || is_null(@.price)))]`)) fmt.Println("Exists:") for _, node := range exists { fmt.Printf( " > %s - %v\n", node.MustKey("title").String(), must(node.MustKey("price").Value()), ) } fmt.Println("NOT Exists:") for _, node := range NOT_exists { fmt.Printf(" > %s", node.MustKey("title").String()) } } func try(err error) { if err != nil { panic(err) } } func must[T any](v T, err error) T { try(err) return v } const example = ` { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century" }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 0 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": null }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": false } ] } }`
Output:
Exists: > "Sword of Honour" - 0 > "Moby Dick" - <nil> > "The Lord of the Rings" - false NOT Exists: > "Sayings of the Century"
And so, in your case, I would suggest use:
$> ajson '(@.a || @.a == false || @.a == 0 || is_null(@.a))' x.json true
$> ajson '(@.b || @.b == false || @.b == 0 || is_null(@.b))' x.json false
$> ajson 'not(@.b || @.b == false || @.b == 0 || is_null(@.b))' x.json true
Sorry, something went wrong.
No branches or pull requests
the go code works the same way, if the given key doesn't exist, its value is not
null
, andis_null()
check is false.so is there any way to check this? or a new function helper is needed?
The text was updated successfully, but these errors were encountered: