Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
encoding/json: include field name in unmarshal error messages #6716
Before filing a bug, please check whether it has been fixed since the latest release. Search the issue tracker and check that you're running the latest version of Go: Run "go version" and compare against http://golang.org/doc/devel/release.html If a newer version of Go exists, install it and retry what you did to reproduce the problem. Thanks. What steps will reproduce the problem? If possible, include a link to a program on play.golang.org. 1. Run http://play.golang.org/p/6UeIwpPMGU 2. 3. What is the expected output? An error message indicating which field caused the unmarshaller to choke. Something like "json: cannot unmarshal number into Go value of type string for key/name 'Zip'" What do you see instead? "json: cannot unmarshal number into Go value of type string" Which compiler are you using (5g, 6g, 8g, gccgo)? default compiler Which operating system are you using? OS X 10.9 (13A603) Which version are you using? (run 'go version') go version go1.2rc3 darwin/amd64 Please provide any additional information below. In the example I provided, it is trivial to deduce that either the programmer mistakenly defined the Zip code field to be string when it should have been an int or the input json received was invalid. In practice, json objects are much larger and more complex. Determining which field of the hundreds in the message caused the unmarshaller to choke is time consuming and painful. The task could be made much easier if the field name were displayed as part of the error message making it easier to track down bugs.
Agree this would be nice. Made slightly inconvenient to implement because by the time the error is generated the decoder is looking at the field value itself, not the struct that contains the field. The relevant code doesn't even know that it's decoding into a struct field. There are a few ways to get around this but they are non-trivial.
Labels changed: added priority-later, removed priority-triage.
Status changed to Accepted.
referenced this issue
Nov 17, 2017
referenced this issue
Nov 20, 2017
I was complaining about this error message:
However, this turns out to probably be a bug in the go-protoc generated code for protobufs, rather than go json itself.