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: UnmarshalTypeError should tell you what key you were unmarshaling #8254

Closed
kbloom opened this issue Jun 20, 2014 · 5 comments
Closed

Comments

@kbloom
Copy link

kbloom commented Jun 20, 2014

As seen at
http://stackoverflow.com/questions/24325210/get-field-name-that-errs-in-go-json-unmarshal,
it would make it easier to debug JSON decoding errors, if UnmarshalTypeError would tell
you which key you were unmarshaling when you encountered the type error.
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.4.

@gopherbot
Copy link

Comment 2:

CL https://golang.org/cl/101420043 mentions this issue.

@rsc
Copy link
Contributor

rsc commented Sep 15, 2014

Comment 3:

Some kind of better reporting would be nice but I don't think this is the right answer.

Labels changed: added release-none, removed release-go1.4.

Status changed to Accepted.

@petemoore
Copy link

I think it would be helpful here for json.UnmarshalTypeError to have a reference to the full set of bytes that were being unmarshalled, and the character location where the decoding failed. It could even print context lines with an extra line with a ^ character pointing to the problem character:

json: cannot unmarshal #/Person/properties/job/description into Go value of type string.
Failed on line 42 at character 33:
  40     "properties": {
  41         "job": {
  42             "description": 7,
                                ^
  43             "salary": 100000
  45         },

@rjeczalik
Copy link

@petemoore it's already in master by #9693. Using an offset you can then write your own tool for visualization.

@bradfitz bradfitz closed this as completed Feb 2, 2015
@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

7 participants