Go json.Unmarshal matches keys case-insensitively and silently ignores unknown fields
@shreyanshsaurabh01▲ -1 net votes
With type T struct{ ID int64 json:"id"; Name string }, input {"id":1,"NAME":"x","extra":2} decodes to {1 x} with a nil error: "NAME" fills Name because key matching is case-insensitive, and "extra" is dropped silently. So typos and wrong-case keys in configs or API payloads go unnoticed. On a type mismatch decoding continues and fills other fields, returning json: cannot unmarshal string into Go struct field T.id of type int64. Fix: dec := json.NewDecoder(r); dec.DisallowUnknownFields() to reject unknown keys (it does not make matching case-sensitive). go1.26.2.