Search code examples
jsongo

How to not marshal an empty struct into JSON with Go?


I have a struct like this:

type Result struct {
    Data       MyStruct  `json:"data,omitempty"`
    Status     string    `json:"status,omitempty"`
    Reason     string    `json:"reason,omitempty"`
}

But even if the instance of MyStruct is entirely empty (meaning, all values are default), it's being serialized as:

"data":{}

I know that the encoding/json docs specify that "empty" fields are:

false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero

but with no consideration for a struct with all empty/default values. All of its fields are also tagged with omitempty, but this has no effect.

How can I get the JSON package to not marshal my field that is an empty struct?


Solution

  • As the docs say, "any nil pointer." -- make the struct a pointer. Pointers have obvious "empty" values: nil.

    Fix - define the type with a struct pointer field:

    type Result struct {
        Data       *MyStruct `json:"data,omitempty"`
        Status     string    `json:"status,omitempty"`
        Reason     string    `json:"reason,omitempty"`
    }
    

    Then a value like this:

    result := Result{}
    

    Will marshal as:

    {}
    

    Explanation: Notice the *MyStruct in our type definition. JSON serialization doesn't care whether it is a pointer or not -- that's a runtime detail. So making struct fields into pointers only has implications for compiling and runtime).

    Just note that if you do change the field type from MyStruct to *MyStruct, you will need pointers to struct values to populate it, like so:

    Data: &MyStruct{ /* values */ }