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

No error with incorrect path and missing field #25

Open
Ezri-Mudde opened this issue May 26, 2020 · 0 comments
Open

No error with incorrect path and missing field #25

Ezri-Mudde opened this issue May 26, 2020 · 0 comments

Comments

@Ezri-Mudde
Copy link

When path doesn't begin with a $ Get doesn't return an error when the field doesn't exist, Get does return an error when path begins with $. See example below or use playground

package main

import (
	"encoding/json"
	"fmt"
	"github.com/PaesslerAG/jsonpath"
)

func main() {
	v := interface{}(nil)
	json.Unmarshal([]byte(`{
	"welcome":{
			"123456":["Good Morning", "Hello World!"]
		}
	}`), &v)

	if unknownWithoutPrefix, err := jsonpath.Get("nope", v); err != nil {
		fmt.Printf("Error: %s\n", err)
	} else if unknownWithoutPrefix == nil {
		fmt.Println("no value for missing field and no error")
	}

	if knownWithoutPrefix, err := jsonpath.Get("welcome", v); err == nil {
		fmt.Printf("welcome: %v\n", knownWithoutPrefix)
	}

	if unknownWithPrefix, err := jsonpath.Get("$.nope", v); err != nil {
		fmt.Printf("Error: %s\n", err)
	} else {
		fmt.Println(unknownWithPrefix)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant