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.
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.
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:
json.Unmarshall will return a *json.SyntaxError, which has an Offset field, which contains the number of bytes successfully read before the syntax error.