I need to return an empty json {}
when the map is not nil
, but it's empty. When the map is nil
I need it to be omitted.
How could I go about doing this?
type ChildMap map[string]string
type Parent struct {
ID int64
T ChildMap `json:"t,omitempty"`
}
Here's a playground that explains what I'm trying to do quite well:
https://go.dev/play/p/hahseo9nyh3
In 1st case it needs to be omitted (this works), 2nd case I need it returned as {}
(doesn't work), 3rd case needs to be displayed (also works)