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

Still have questions?

Can't find the answer you're looking for? Please reach out to our support team.