Go’s unmarshal errors might not work the way you think they do

Bahar Shah
2 min readFeb 23, 2023

I recently went down a rabbit hole while trying to unmarshal a JSON string into a struct and handle the case where the JSON string I was unmarshaling didn’t match the struct I was attempting to turn it into. The assumption I made here was that the unmarshal would throw an error if the string didn’t match- joke’s on me though! Turns out that json.Unmarshal does not error out when the data provided does not match the syntax you are expecting.

When you’re parsing JSON strings in Go, it probably looks something like this:

var jsonObject MyStruct
file, err := ioutil.ReadFile(jsonFile)
if err != nil {
return MyStruct{}, err
}
err = json.Unmarshal(file, &jsonObject)
if err != nil {
return MyStruct{}, err

Now, the thing you might assume which is what I did was that if the file contents do not match your struct the unmarshal would error, you could handle it there and move on with your coding. Instead, at the heart of the unmarshaling process it ignores any unknown fields and simply skips over them. This results in an empty struct being created in the case where your file contents do not match the struct rather than an explicit error.

If you have a use case where you only care about certain fields in your JSON input but still want to know when none of them match the best solution is to check if the resultant struct is empty.

if reflect.DeepEqual(jsonObject, MyObject{}) {
err = errors.New(“Can’t unmarshal JSON object into struct- format does not match”)
return MyObject{}, err
}

If you want to error out if any part of the JSON input does not match the expected struct you could instead decode the JSON and disallow the unknown fields. This works well if you need to validate the exact JSON input down to every field.

//unmarshalled successfully but with wrong json
decoder := json.NewDecoder(strings.NewReader(jsonObject))
decoder.DisallowUnknownFields()
err = decoder.Decode(&MyObject)
if err != nil {
return err
}

Hope this helps someone else save a few minutes!

--

--

Bahar Shah

Software engineer/manager by day. Baker, reader and wannabe film critic by night.