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

encoding/json: invalid use of ,string struct tag, trying to unmarshal "" into int #41731

andig opened this issue Oct 1, 2020 · 3 comments


Copy link

@andig andig commented Oct 1, 2020

What version of Go are you using (go version)?

$ go version
latest (playground)

Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

Unmarshal json (

s := struct{
	Val int `json:",string,omitempty"`

println(json.Unmarshal([]byte(`{"val":""}`), &s))

What did you expect to see?

Since val is "", the error as such is expected, however I would expect the omitempty to make a difference and hence return nil error. According to

The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

What did you see instead?

Error: invalid use of ,string struct tag, trying to unmarshal "" into int


To give flexibility in handling external json input it is preferable to handle the omitempty tag before handling further validations like string. In this case the order seems to be different.

If effect, ,string,omitempty should allow "" to be unmarshaled into numeric zero values.

UPDATE I realize this might indeed be more a proposal as omitempty seems to be documented and take effect only for marshaling but not unmarshaling.

Copy link

@mvdan mvdan commented Oct 1, 2020

Indeed, omitempty only affects marsaling, not unmarshaling. I think making it also affect unmarshaling is likely a breaking change, because right now unmarshaling an empty string with string `json:",omitempty"` sets the destination to "", whereas with your proposal we would not modify the value at all.

Copy link

@andybons andybons commented Oct 1, 2020

@andig if you’d like this to be a proposal, could you update the first comment and subject accordingly? See for more info. Thanks!

Copy link
Contributor Author

@andig andig commented Oct 2, 2020

Closing this one. I feel the scope I'm asking about is much too narrow for a proper proposal. A proposal if any should consider a wider range of encoding (and potentially decoding) issues.

@andig andig closed this Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.