Friday, March 10, 2023
HomeGolangHow you can know which line of json is failing to unmarshal?...

How you can know which line of json is failing to unmarshal? – Getting Assist


I’m engaged on an app that makes use of go to parse a json file for settings. I get the error proven under, but it surely’s not telling me what line within the json file is inflicting the difficulty. How do I see this?

panic: json: can not unmarshal string into Go worth of kind map[string]*json.RawMessage


You possibly can verify your Go code to see the place you have got a discipline of kind map[string]*json.RawMessage after which verify your JSON file to see if it’s a string as an alternative.

I don’t have entry to the code. I hoped there was a strategy to activate extra detailed logs like in Rust

I’m not conscious of any method to try this in Go.

Might you paste your JSON into an internet JSON parser and see what the offending line is that method? Additionally you possibly can organize a fast take a look at utilizing the playground and attempt to debug it that method. For instance, modify this playground hyperlink to have your precise JSON:

func foremost() {
	// Substitute this together with your JSON
	byt := []byte(`{ "value1": "take a look at", "value2": }`)
	var dat map[string]*json.RawMessage
	if err := json.Unmarshal(byt, &dat); err != nil {
		panic(err)
	}
	fmt.Println(dat)
}

The output from that’s at the very least extra descriptive than what your executable is providing you with:

panic: invalid character '}' on the lookout for starting of worth

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

Program exited.

You possibly can additionally attempt the process outlined right here together with that go playground hyperlink to get extra information:

Although as soon as once more, an internet JSON parser ought to work positive to offer you an in depth error message.

That helps if the JSON is invalid, however in case you put a JSON string worth the place an object is anticipated (e.g. {"key": "string"} vs {"key": {"subkey": "string"}}), I don’t suppose this may assist until you have got a JSON schema definition.

RELATED ARTICLES

Most Popular

Recent Comments