Skip to content

encoding/json: bug in json.Unmarshal if keys in JSON differ only by case #26687

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
AyushG3112 opened this issue Jul 30, 2018 · 7 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@AyushG3112
Copy link

AyushG3112 commented Jul 30, 2018

Please answer these questions before submitting your issue. Thanks!

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

Playground and go version go1.10.2 linux/amd64

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

Playground and:

GOARCH="amd64"
GOBIN=""
GOCACHE= **REDACTED**
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH= **REDACTED**
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build198743377=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Here is the input json

{"a" :"This is value for ASmall", "A": "This is value for ACaps"}

In Playground Link 1, my target struct only has a field corresponding to the a key.

In Playground Link 2, my target struct has fields corresponding to both a and A.

What did you expect to see?

In Link 1, I expected to see the value printed to be This is value for ASmall which corresponds to the value of key a

What did you see instead?

The value printed was This is value for ACaps which corresponds to the value of key A.
Link 2 worked fine.

@mvdan
Copy link
Member

mvdan commented Jul 30, 2018

Reading the godoc, this appears to be incorrect behavior:

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.

If the case insensitive match overwrites the case sensitive match, it seems like the preference mentioned above isn't working.

@mvdan mvdan added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 30, 2018
@mvdan mvdan added this to the Go1.12 milestone Jul 30, 2018
@AyushG3112
Copy link
Author

@mvdan Interestingly, if I switch the order of keys in the JSON, so that the new JSON becomes:

{"A":"This is value for ACaps","a":"This is value for ASmall"}

It works fine. Playground link

@AlexRouSg
Copy link
Contributor

@mvdan I believe this is #14750

@agnivade
Copy link
Contributor

/cc @dsnet

@mvdan
Copy link
Member

mvdan commented Jul 30, 2018

Indeed seems like a duplicate - thanks @AlexRouSg.

@mvdan mvdan closed this as completed Jul 30, 2018
@lozhn
Copy link

lozhn commented Jul 30, 2018

@AyushG3112 not it's not - https://play.golang.org/p/uyYYa186mez - the latter is used

@cespare cespare changed the title enconding/json: bug in json.Unmarshal if keys in JSON differ only by case encoding/json: bug in json.Unmarshal if keys in JSON differ only by case Jul 31, 2018
@AyushG3112
Copy link
Author

@npenzin I already saw that, refer my first reply to this issue after the OP. But again, for a valid JSON with a valid target struct, the order of keys should not matter.

@golang golang locked and limited conversation to collaborators Jul 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants