How to know which line of json is failing to unmarshal?

I’m working on an app that uses go to parse a json file for settings. I get the error shown below, but it’s not telling me what line in the json file is causing the issue. How do I see this?

panic: json: cannot unmarshal string into Go value of type map[string]*json.RawMessage

You could check your Go code to see where you have a field of type map[string]*json.RawMessage and then check your JSON file to see if it’s a string instead.

I don’t have access to the code. I was hoping there was a way to turn on more detailed logs like in Rust

I’m not aware of any way to do that in Go.

Could you paste your JSON into an online JSON parser and see what the offending line is that way? Also you could arrange a quick test using the playground and try to debug it that way. For example, modify this playground link to have your actual JSON:

func main() {
	// Replace this with your JSON
	byt := []byte(`{ "value1": "test", "value2": }`)
	var dat map[string]*json.RawMessage
	if err := json.Unmarshal(byt, &dat); err != nil {
		panic(err)
	}
	fmt.Println(dat)
}

The output from that is at least more descriptive than what your executable is giving you:

panic: invalid character '}' looking for beginning of value

goroutine 1 [running]:
main.main()
	/tmp/sandbox266866198/prog.go:13 +0xe7

Program exited.

You could also try the procedure outlined here in combination with that go playground link to get more info:

Though once again, an online JSON parser should work fine to give you a detailed error message.

That helps if the JSON is invalid, but if you put a JSON string value where an object is expected (e.g. {"key": "string"} vs {"key": {"subkey": "string"}}), I don’t think this will help unless you have a JSON schema definition.

1 Like

Ah good call - I incorrectly assumed it was invalid JSON because map[string]*json.RawMessage is pretty flexible (it can handle both the examples you mentioned just fine). But you’re totally right - it’s valid JSON to have only an array as top-level text:

And in that case, map[string] would break.

json.Unmarshall will return a *json.SyntaxError, which has an Offset field, which contains the number of bytes successfully read before the syntax error.

To identify the line in your JSON file that is causing the error, you can use a JSON validator or formatter tool. These tools can help you identify syntax errors in your JSON file by highlighting the problematic line or showing you an error message that describes the issue.

It turns out that this error is a *json.UnmarshalTypeError and that type has an Offset field that will tell you where the parser was when the error happened. It should point to the end of the value that failed to unmarshal: The Go Play Space

print log to show json string

When using a JSON unmarshaling library or function, check the error messages provided during the unmarshaling process. The error message might include information about the specific line, field, or element that caused the failure. This can give you a starting point for locating the issue in your JSON data.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.