Configuring JSON serialization
suggest changeHide/skip certain fields
To keep a field on the struct but omit it from encoding and decoding, give an exported field the tag json:"-" or leave the field unexported (a name that starts with a lowercase letter). An unexported field is also invisible to other packages. encoding/json ignores unexported fields.
type Company struct {
Name string `json:"name"`
Location string `json:"location"`
Revenue int `json:"-"`
sales int
}
Ignore empty fields
To prevent Location from being included in the JSON when it is set to its zero value, add ,omitempty to the json tag.
type Company struct {
Name string `json:"name"`
Location string `json:"location,omitempty"`
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents