Go JSON
Why is omitempty not working for my Go struct fields?
The omitempty tag only omits fields with their zero value: 0 for numbers, false for booleans, empty string for strings, and nil for pointers. It does not omit zero values for struct types—structs are never considered empty. To make struct fields truly optional, use pointers: *MyStruct with omitempty omits nil structs. For booleans, false is zero so omitempty skips it; use *bool if you need to distinguish absent from false. Custom types need MarshalJSON implementation to define emptiness. Empty slices and maps are omitted, but check initialization versus nil. Zero-value numbers (0) are omitted which may not be desired behavior. Verify tag syntax: `json:"field,omitempty"` not `json:"field, omitempty"` (no space). Test your struct marshaling output with our JSON Viewer at jsonconsole.com/json-viewer to see what actually serializes. Use pointers for optional fields when zero values are meaningful. Understanding zero values is key to using omitempty effectively.
Last updated: December 23, 2025
Previous
How do I use Go struct tags to rename JSON fields?
Next
When should I use json.NewDecoder vs json.Unmarshal in Go?
Related Questions
How do I use Go struct tags to rename JSON fields?
Learn how to use Go struct tags to rename JSON fields. Master JSON marshaling with proper field naming and omitempty.
When should I use json.NewDecoder vs json.Unmarshal in Go?
Learn when to use json.NewDecoder vs json.Unmarshal in Go. Understand streaming vs in-memory JSON parsing.
How can I ignore specific fields during JSON marshaling in Go?
Learn how to ignore fields during JSON marshaling in Go. Master struct tags, custom marshaling, and data hiding techniques.
Still have questions?
Can't find the answer you're looking for? Please reach out to our support team.