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

Json Marshal considers value '0' as nil #19065

Closed
mrajashree opened this issue Feb 13, 2017 · 2 comments
Closed

Json Marshal considers value '0' as nil #19065

mrajashree opened this issue Feb 13, 2017 · 2 comments

Comments

@mrajashree
Copy link

Hi, during json.Marshal, a 0 value for an int is considered as empty, and the corresponding struct field isn't even made a part of the output. For example, if this is my struct

type some struct {
	Name string `json:"name,omitempty"`
	Value int64 `json:"value,omitempty"`
}

And this is where I'll marshal it:

someEx := &some{
	Name: "blah",
	Value: 0,
}
bodyContent, _ := json.Marshal(someEx)

The value of string(bodyContent) will be {"name":"blah"}. But when I use the following value

someEx := &some{
	Name: "blah",
	Value: 1,
}

Then the value of string(bodyContent) is {"name":"blah","value":1}.
I think that even if the value is 0, it can be needed by some applications, is it possible to pass it along?

@bradfitz
Copy link
Contributor

Just drop the ,omitempty part?

For questions about Go, see https://golang.org/wiki/Questions.

If this is a bug report, though, feel free to reopen this.

@mrajashree
Copy link
Author

Ah my bad. Don't know how I didn't notice that

@golang golang locked and limited conversation to collaborators Feb 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants