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 do I check for null values? #9

Open
jmcmahon opened this issue Jul 19, 2018 · 2 comments
Open

How do I check for null values? #9

jmcmahon opened this issue Jul 19, 2018 · 2 comments

Comments

@jmcmahon
Copy link

jmcmahon commented Jul 19, 2018

How do I check if the address field below is null?

package main

import (
	"fmt"
	"github.com/smallfish/simpleyaml"
)

var data = []byte(`
name: smallfish
age: 99
bool: true
bb:
    cc:
        dd:
            - 111
            - 222
            - 333
address: null
`)

func main() {
	y, err := simpleyaml.NewYaml(data)
	if err != nil {
		// ERROR
	}

	if v, err := y.Get("name").String(); err == nil {
		fmt.Println("value:", v)
	}

  if v, err := y.Get("age").Int(); err == nil {
    fmt.Println("value:", v)
  }

  if v, err := y.Get("bool").Bool(); err == nil {
    fmt.Println("value:", v)
  }
  
  if v, err := y.Get("bb").Get("cc").Get("dd").Array() ; err == nil {
    fmt.Println("value:", v)
  }

  if v, err := y.Get("bb").Get("cc").Get("dd").GetIndex(1).Int() ; err == nil {
    fmt.Println("value:", v)
  }

  if v, err := y.GetPath("bb", "cc", "dd").Array() ; err == nil {
    fmt.Println("value:", v)
  }


}

@jmcmahon
Copy link
Author

@jmcmahon
Copy link
Author

in my case, referring to an explicit null value as described here:

https://www.calhoun.io/how-to-determine-if-a-json-key-has-been-set-to-null-or-not-provided/

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