Skip to content
New issue

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

how to check the given field is missing from the given object? #83

Open
Yonokawa opened this issue Dec 23, 2024 · 1 comment
Open

how to check the given field is missing from the given object? #83

Yonokawa opened this issue Dec 23, 2024 · 1 comment
Labels
question Further information is requested

Comments

@Yonokawa
Copy link

$ 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.

so is there any way to check this? or a new function helper is needed?

@spyzhov
Copy link
Owner

spyzhov commented Dec 26, 2024

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

@spyzhov spyzhov added the question Further information is requested label Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants