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: clarify merge semantics of Unmarshal #27172

gpaul opened this issue Aug 23, 2018 · 3 comments · May be fixed by #28735

encoding/json: clarify merge semantics of Unmarshal #27172

gpaul opened this issue Aug 23, 2018 · 3 comments · May be fixed by #28735


Copy link

@gpaul gpaul commented Aug 23, 2018

Please answer these questions before submitting your issue. Thanks!

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

Go playground

Does this issue reproduce with the latest release?


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

Go playground

What did you do?

What did you expect to see?


What did you see instead?

{john }
{john smith}

In short...

The Unmarshal documentation says:

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.


I assumed that after Unmarshal returns, the struct would exactly match the provided JSON. I found this quite surprising and I think the documentation should mention that "Fields that are not present in the JSON document will retain their original values."

The existing functionality does give one an elegant way to set default values by having Unmarshal only update those fields which are present in the JSON document. I do not dispute that it is useful. Still, I would like to see the documentation mention this behaviour explicitly.

hellozee added a commit to hellozee/go that referenced this issue Nov 12, 2018
The existing documentation misses to cover what will happen if json
data with missing fields is unmarshalled, so a line is added to the
documentation of json.Unmarshall() mentioning the consequences of
unmarshalling json fields.

Fixes golang#27172
Copy link

@gopherbot gopherbot commented Nov 12, 2018

Change mentions this issue: encoding/json: document the unmarshalling of missing json fields


Copy link

@gopherbot gopherbot commented Feb 26, 2020

Change mentions this issue: encoding/json: clarify how we decode into structs


Copy link

@mvdan mvdan commented Feb 26, 2020

The first CL hasn't been updated after the last round of reviews, and I'm tackling a related documentation issue as well, so I've sent a CL of my own.


@dsnet dsnet changed the title encoding/json.Unmarshal: clarify documentation around pre-populated field values encoding/json: clarify merge semantics of Unmarshal Dec 31, 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.

3 participants