snippetgoCritical
How to not marshal an empty struct into JSON with Go?
Viewed 0 times
emptywithhowstructnotintomarshaljson
Problem
I have a struct like this:
But even if the instance of MyStruct is entirely empty (meaning, all values are default), it's being serialized as:
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
How can I get the JSON package to not marshal my field that is an empty struct?
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:
Fix - define the type with a struct pointer field:
Then a value like this:
Will marshal as:
Explanation: Notice the
Just note that if you do change the field type from
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 */ }Code Snippets
type Result struct {
Data *MyStruct `json:"data,omitempty"`
Status string `json:"status,omitempty"`
Reason string `json:"reason,omitempty"`
}result := Result{}Data: &MyStruct{ /* values */ }Context
Stack Overflow Q#18088294, score: 238
Revisions (0)
No revisions yet.