Golang JSON Omitempty: Effortlessly Exclude Empty Fields for Cleaner Serialization
type User struct {
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
Active bool `json:"active,omitempty"`
Hobbies []string `json:"hobbies,omitempty"`
}
type Address struct {
City string `json:"city,omitempty"`
} type User struct {
Name string `json:"name,omitempty"`
Address Address `json:"address,omitempty"`
}
type Order struct {
ID string `json:"id,omitempty"`
Customer *Customer `json:"customer,omitempty"`
Items []*OrderItem `json:"items,omitempty"`
}
type Customer struct {
PrimaryAddress *Address `json:"primary_address,omitempty"`
BackupAddress *Address `json:"backup_address,omitempty"`
}