Code Beautifier
json: unknown field

Fix "json: unknown field" in Go

Only raised when DisallowUnknownFields is on: the payload has a key the struct does not declare. Whether to add the field, ignore it, or stay strict.

Input that triggers it

{"id": 1, "name": "Ada", "nickname": "countess"}
Open JSON to Go on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

json
Go structsgo

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: nickName versus nickname. Go's matching is case-insensitive for exact names, but nick_name and nickname are different fields.
  • Strictness enabled for a config file, where an unknown key really is a mistake worth failing on.

How to fix it

  1. 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.
  2. 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.
  3. If you drop strict mode, remove the DisallowUnknownFields() call; the default decoder ignores the extra key.
  4. 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