Standard library / encoding/json

JSON struct tags

JSON struct tags rename exported fields, omit empty values with omitempty, or suppress fields with -. The Go 1.23 omitempty rule is not a general zero-value rule for structs. Expected stdout: '{"name":"Ada"}\n'.

Formal Syntax & Structure

go syntaxgo
`json:"name,omitempty"` / `json:"-"`

Working Example

Example Snippetgo
package main
import (  "encoding/json"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  x := struct {    Name   string `json:"name"`    Note   string `json:"note,omitempty"`    Secret string `json:"-"`  }{Name: "Ada", Secret: "private"}  b, e := json.Marshal(x)  must(e)  fmt.Println(string(b))}
Go Reference