What the error means
By default, encoding/json ignores keys in the input that have no matching struct field. That is convenient and also dangerous: a typo in a key name silently drops the value. json.Decoder.DisallowUnknownFields() switches the decoder to strict mode, and json: unknown field "nickname" is its response when the payload contains a key the struct does not declare.
So the error only appears if someone turned strictness on deliberately, usually in a config loader or an API handler where an unexpected key should be a hard failure. In the example, nickname is present in the payload and absent from a struct generated from a sample without it.
Why it happens
- The struct was generated from an incomplete sample. Optional keys that were absent from the sample are absent from the struct.
- The producer added a field in a newer version of the API and the consumer is still on the old struct.
- A typo on either side:
nickNameversusnickname. Go's matching is case-insensitive for exact names, butnick_nameandnicknameare different fields. - Strictness enabled for a config file, where an unknown key really is a mistake worth failing on.
How to fix it
- Decide whether strictness is wanted here. For a configuration file, it usually is: an unknown key means a typo or an option that no longer exists, and failing loudly is the point. For an API response, it usually is not: the producer is allowed to add fields.
- If you keep strict mode, add the field. Paste a payload that includes every key into the generator above; it produces a struct with all of them, and marks fields that are missing from some samples with
omitempty. - If you drop strict mode, remove the
DisallowUnknownFields()call; the default decoder ignores the extra key. - If the key is a typo on the producer's side, fix the producer; a struct that accepts both spellings hides the bug.
The struct that accepts the example under strict mode:
type Root struct {
ID int64 `json:"id"`
Name string `json:"name"`
Nickname string `json:"nickname,omitempty"`
}
If it still fails
- Compare the failing payload with the sample you generated from using JSON Diff; the added keys are listed by path.
- Jackson's equivalent is
UnrecognizedPropertyException, which is strict by default — the opposite of Go. JSON to Java explains the annotation that relaxes it. - Unknown-field errors inside nested structs name the full path in the message; the fix is the same, one level down.
Related errors
json: cannot unmarshal string into Go struct field
Make the struct field match the payload (string), or fix the producer to send a number; for numbers-as-strings, tag the field with `json:",string"` or unmarshal into json.Number.
UnrecognizedPropertyException: Unrecognized field
Add the field to the class (regenerate from a sample that includes it), annotate the class with @JsonIgnoreProperties(ignoreUnknown = true), or disable FAIL_ON_UNKNOWN_PROPERTIES on the mapper.