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: decoding a null value does not replace previous value #14640

AntiPaste opened this issue Mar 4, 2016 · 4 comments

encoding/json: decoding a null value does not replace previous value #14640

AntiPaste opened this issue Mar 4, 2016 · 4 comments


Copy link

@AntiPaste AntiPaste commented Mar 4, 2016

Decoding a JSON value into an already populated struct replaces the previous value stored in the struct as long as the key is present.

Example of working behaviour:

However, if the decoded value is null the previous value is not replaced.

Example of incorrect behaviour:

Is this intended behaviour for json.Unmarshal, json.Decoder.Decode & co.?

Additionally, if this is intended behaviour the json.Decoder.Decode example at might need fixing. It currently shows the following example which will exhibit incorrect results if either Name or Text is null:

A fixed version without possibility of incorrect results:

Go version: go version go1.6 linux/amd64
Go environment:

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
@ianlancetaylor ianlancetaylor changed the title encoding/json: Decoding a null value does not replace previous value encoding/json: decoding a null value does not replace previous value Mar 4, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Mar 4, 2016
Copy link

@cespare cespare commented May 14, 2016

I believe this is working as intended and documented.

Unmarshaling null into a string intentionally does not change the string:

The JSON null value unmarshals into an interface, map, pointer, or slice by setting that Go value to nil. Because null is often used in JSON to mean “not present,” unmarshaling a JSON null into any other Go type has no effect on the value and produces no error.

Example without a struct:

If your struct has one of the nullable types mentioned above, such as a pointer, then unmarshaling null will set your field to nil:

Copy link

@rsc rsc commented May 18, 2016

The docs @cespare wrote are very clear. I am not really a fan of this behavior but it's widely expected and too late to change.

Copy link

@quentinmit quentinmit commented May 26, 2016

It seems like this is just a matter of improving the example code, then. I sent CL 23483 for that.

Copy link

@gopherbot gopherbot commented May 26, 2016

CL mentions this issue.

@gopherbot gopherbot closed this in b1894bb May 26, 2016
@golang golang locked and limited conversation to collaborators May 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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