How to Use Struct Tags for JSON in Go

Use backtick-delimited struct tags with the json key to rename fields, omit empty values, or exclude data during JSON marshaling.

Use struct tags to control JSON field names, aliases, and inclusion by adding backtick-delimited metadata to struct fields. The json tag accepts a field name, an omitempty option to skip zero values, and an inline option to embed structs.

type User struct {
    Name  string `json:"name"`
    Email string `json:"email,omitempty"`
    Age   int    `json:"-"`
}