You can configure how a type is marshaled by implementing an json.Marshalerinterface . This overrides the default behavior for introspecting structure fields.
In this specific example, you can do something like:
func (s MyStruct) MarshalJSON() ([]byte, error) {
data := map[string]interface{}{
key: s.field,
}
return json.Marshal(data)
}
Here I create a value map[string]interface{}that represents what I want in the JSON output, and pass it to json.Marshal.
You can check this example here: http://play.golang.org/p/oTmuNMz-0e
source
share