Skip to content

json.Unmarshal() documentation doesn't make it clear that struct fields need to be exported #5965

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

Closed
gopherbot opened this issue Jul 26, 2013 · 1 comment

Comments

@gopherbot
Copy link
Contributor

by timothy.l.jones:

The json.Unmarshal() documentation doesn't make it clear that struct fields need to be
exported for Unmarshal to work correctly. This is only documented at
http://golang.org/doc/articles/json_and_go.html , but perhaps it should also be
documented in the json API.

* What steps will reproduce the problem?
1. visit http://golang.org/pkg/encoding/json/#Unmarshal
2. The documentation says "To unmarshal JSON into a struct, Unmarshal matches
incoming object keys to the keys used by Marshal (either the struct field name or its
tag), preferring an exact match but also accepting a case-insensitive match."
3. This implies that the following would work:

type Blah struct {
  name string
  num float64
}
....
var result Blah
err := json.Unmarshal(blob,&result)
 
See http://play.golang.org/p/P2CWHLa3jt

But instead, the struct needs to be changed to:

type Blah struct {
  Name string   // Note the uppercase field names
  Num float64
}

* What is the expected output?

I would expect that the documentation in http://golang.org/pkg/encoding/json/#Unmarshal
says something like "note that Unmarshal is only able to access exported struct
fields"

What do you see instead?

This information is only available in the article at
http://golang.org/doc/articles/json_and_go.html

Which compiler are you using (5g, 6g, 8g, gccgo)?
N/A

Which operating system are you using?
N/A

Which version are you using?  (run 'go version')
Online doco.

Please provide any additional information below.
@robpike
Copy link
Contributor

robpike commented Jul 26, 2013

Comment 1:

The docs say that Unmarshal uses the same rules as Marshal, and Marshal is clear that
only exported fields are encoded. In other words, it is documented in the JSON API,
despite the claim made in the issue.
Perhaps some of the key points should be reiterated, but the documentation is already
long.

Status changed to WontFix.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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

2 participants