jsonpath icon indicating copy to clipboard operation
jsonpath copied to clipboard

No error with incorrect path and missing field

Open Ezri-Mudde opened this issue 5 years ago • 0 comments

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)
	}
}

Ezri-Mudde avatar May 26 '20 13:05 Ezri-Mudde