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: bug in json.Unmarshal if keys in JSON differ only by case #26687

Closed
AyushG3112 opened this issue Jul 30, 2018 · 7 comments

Comments

Projects
None yet
5 participants
@AyushG3112
Copy link

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

This comment has been minimized.

Copy link
Member

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 this to the Go1.12 milestone Jul 30, 2018

@AyushG3112

This comment has been minimized.

Copy link
Author

commented Jul 30, 2018

@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

This comment has been minimized.

Copy link
Contributor

commented Jul 30, 2018

@mvdan I believe this is #14750

@agnivade

This comment has been minimized.

Copy link
Member

commented Jul 30, 2018

/cc @dsnet

@mvdan

This comment has been minimized.

Copy link
Member

commented Jul 30, 2018

Indeed seems like a duplicate - thanks @AlexRouSg.

@mvdan mvdan closed this Jul 30, 2018

@lozhn

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Author

commented Jul 31, 2018

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.