Go json tags, omitempty, and the zero value trap
Why every generated Go struct has tags, what omitempty really drops, and why a pointer is sometimes the only honest type.
Generate Go from a JSON sample and every field carries a tag:
type Root struct {
ID int64 `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
}
Three things about Go's JSON make those tags, and the choices around them, worth understanding.
Why the tag is not optional
Go matches a struct field to a JSON key by the tag. Without a tag it falls
back to the field name, case-insensitively — so Name would still find
"name". But UserID would look for "userid", not "user_id", and find
nothing.
Worse, a field has to be exported — capital first letter — for
encoding/json to touch it at all. A lower case field is invisible to the
reader and the writer, with no error. So the tag is doing two jobs: it lets
the field be capitalised the way Go wants, and it keeps the real key.
Never delete a tag. If you rename UserID to ID and drop the tag, the
field is silently always zero.
What omitempty actually drops
Turn on Add omitempty and the tags become:
Name string `json:"name,omitempty"`
When you write JSON, the key disappears if the value is empty. Go's idea of empty is wider than most people expect:
| Type | Dropped when |
|---|---|
| string | "" |
| int, float | 0 |
| bool | false |
| pointer, slice, map | nil |
| slice, map | also when empty, length 0 |
Read that table again with a real field in mind. A Count int with
omitempty never sends "count": 0. A Published bool never sends
"published": false. If the receiving side treats a missing key as "unknown"
or as "use the default", you have just changed the meaning of your message.
Use omitempty for fields that really are optional, and never for a bool or
a counter where zero is a real value.
Go 1.24 added omitzero, which is the one most people wanted: it drops only
the true zero value of the type and leaves an empty slice alone.
The zero value trap, when reading
Go has no optional type. A key that is missing and a key that holds the zero value produce exactly the same struct:
// Both give Count == 0
// {"count": 0}
// {}
Most of the time that is fine. When it is not — a discount that may be absent, a flag that defaults to true, a field that the API removed last week — the only honest type is a pointer:
Count *int `json:"count"`
Now nil means missing and &0 means zero, and your code has to say which it
means. The generator uses a pointer for any field it saw as missing or null in
your samples, which is why giving it more than one sample matters.
A json.RawMessage is the other tool, for a field you want to keep but not
decode yet.
Numbers
encoding/json reads a JSON number into float64 when the target is
interface{}. Into an int64 field it reads the integer correctly, which is
why the generated struct uses int64 and not int.
If you decode into a map or an any, use a decoder with UseNumber() and you
get json.Number, which keeps the digits as text until you choose. Otherwise
a large id loses its last digits. See the number that does not
fit.
Time
A string in RFC 3339 form decodes straight into time.Time, with no help.
Anything else — an epoch number, a date with no time, a format of somebody's
own — needs a type with its own UnmarshalJSON. The generator gives you
time.Time when it recognises the format and int64 when it does not, which
is the honest answer.
The options worth setting
| Option | Use it when |
|---|---|
| Types only | Your project already has its reading code |
| Types and package only | The same, but keep the package line |
| Package name | Always. main is rarely what you want |
| Add omitempty | Only when a missing key is meaningful to the receiver |
| One file for each type | A large schema that you want split |
Try it
Paste a sample with a zero, a false and an empty string, generate with and without Add omitempty, and look at what the writer would send. That one comparison prevents the most common Go JSON bug there is.
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to CodeRead next
Pydantic or a dataclass? Python types from JSON
A dataclass with type hints checks nothing at all. Pydantic checks everything. Here is when each one is the right answer.
Pydantic or a dataclass? Python types from JSON
A dataclass with type hints checks nothing at all. Pydantic checks everything. Here is when each one is the right answer.