how to get the json line number when getting unmarshal errors?

575 views
Skip to first unread message

dave

unread,
Feb 6, 2023, 6:22:29 PM2/6/23
to golang-nuts
I'm getting the error 

panic: json: cannot unmarshal string into Go value of type map[string]*json.RawMessage but it does not tell me which line or section of my json is causing the issue. How can I see this?

Axel Wagner

unread,
Feb 6, 2023, 7:51:06 PM2/6/23
to dave, golang-nuts
The best method I've found is to wrap the input reader in something that keeps track of lines. You can then type-assert the errors to one of the exported error types in the json package to get the offset the error occured and translate it into line numbers using your wrapper.
I have a package for such a wrapper here: https://pkg.go.dev/gonih.org/lines though it's not particularly well tested.

On Tue, Feb 7, 2023 at 12:22 AM dave <david...@philabs.xyz> wrote:
I'm getting the error 

panic: json: cannot unmarshal string into Go value of type map[string]*json.RawMessage but it does not tell me which line or section of my json is causing the issue. How can I see this?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5873fef8-9787-4a5a-b543-61f6f313def6n%40googlegroups.com.

Ian Lance Taylor

unread,
Feb 7, 2023, 12:03:34 AM2/7/23
to Axel Wagner, dave, golang-nuts
On Mon, Feb 6, 2023 at 4:50 PM 'Axel Wagner' via golang-nuts
<golan...@googlegroups.com> wrote:
>
> The best method I've found is to wrap the input reader in something that keeps track of lines. You can then type-assert the errors to one of the exported error types in the json package to get the offset the error occured and translate it into line numbers using your wrapper.
> I have a package for such a wrapper here: https://pkg.go.dev/gonih.org/lines though it's not particularly well tested.
>
> On Tue, Feb 7, 2023 at 12:22 AM dave <david...@philabs.xyz> wrote:
>>
>> I'm getting the error
>>
>> panic: json: cannot unmarshal string into Go value of type map[string]*json.RawMessage but it does not tell me which line or section of my json is causing the issue. How can I see this?

Note that it probably not JSON that is calling panic. You are
probably taking the error returned by JSON and passing it to panic.
That error value that JSON returned actually contains the exact
position of the error. It's just not included in the string returned
by the Error method. You can get the position by writing code like
err.(*json.UnmarshalTypeError).Offset. See
https://pkg.go.dev/encoding/json#UnmarshalTypeError .

Ian

Rich

unread,
Feb 7, 2023, 3:38:09 PM2/7/23
to golang-nuts
I use a tool called hjson 
This not only lets you put comments into the code but many times can be used to fix json. Some of my tools the user has to generate a JSON payload then send it with my tool. I will take their json and run it through hjson first. It doesn't catch everything, but it has fixed quite a few json files that had little things like a missing comma. 

Reply all
Reply to author
Forward
0 new messages