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: doesn't deserialize json fields with underscores into struct's fields who's tag contains the underscore but the struct field name does not #9841

Closed
coreyog opened this issue Feb 11, 2015 · 1 comment

Comments

@coreyog
Copy link

coreyog commented Feb 11, 2015

Json:

{ Some_ID: 4 }

Go:

type Example struct {
    SomeID int "Some_ID"
}
...
data := <above json as []byte>
var e Example
json.Unmarshal(data, &e)

Given the above json and struct, the value e.SomeID will not be filled with the value 4.

From the first of three rules listed at http://blog.golang.org/json-and-go:

[Unmarshal will look through the destination struct's fields to find] An exported field with a tag of [the json field's name]

If I change the struct definition to

type Example struct {
    Some_ID int "Some_ID"
}

Then, after Unmarshalling, e.Some_ID will contain 4 as expected.

It appears that when an underscore is involved in the field in the json, tagging the struct's field with an underscore when the struct's field does not contain an underscore will not marshal that field. If the json contains an underscore, so must the struct's field and the tag is useless.

@bradfitz
Copy link
Contributor

Your struct tag has the wrong syntax, but the issue tracker is not a support forum. Please ask these sorts of questions on the go-nuts@ mailing list or StackOverflow.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
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