Omitting empty fields when encoding
Omitting empty fields from JSON encoding usually saves space and makes the JSON more reader-friendly. However, what is meant by “empty” should be clear.
How to do it...
Use the ,omitempty
JSON tag to omit empty string values, zero integer/floating-point values, zero time.Duration
values, and nil
pointer values.
The ,omitempty
tag does not work for time.Time
values. Use *time.Time
and set it to nil
to omit empty time values:
type Config struct { ... Type string `json:"type,omitempty"` IntValue int `json:"intValue,omitempty"` FloatValue float64 `json:"floatValue,omitempty"` When *time.Time `json:"when...