Essential Go JSON  Suggest an edit

Configuring JSON serialization

Hide/skip certain fields

To export Revenue and Sales, but hide them from encoding/decoding, use json:"-" or rename the variable to begin with a lowercase letter. Note that this prevents the variable from being visible outside the package.

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"`
}
  ↑ ↓ to navigate     ↵ to select     Esc to close