package main import ( "encoding/json" "fmt" "html/template" "log" "os" ) type Optional[T any] struct { Value T Exists bool } func (o *Optional[T]) UnmarshalJSON(data []byte) error { var Temp *T err := json.Unmarshal(data, &Temp) if err != nil { return err } if Temp == nil { var empty T o.Value = empty o.Exists = false return nil } o.Value = *Temp o.Exists = true return nil } func (o Optional[T]) MarshalJSON() ([]byte, error) { var Temp *T if o.Exists { Temp = &o.Value } return json.Marshal(Temp) } type Testy struct { Flag *bool `json:"flag"` Cake *[]string `json:"cake"` Opt Optional[string] `json:"opt"` } const TEXT = `{ "cake": [ "one", "two", "three" ], "opt": "peter" }` const TEMPLATE = `Flag is: {{if .Flag}}{{.Flag}}{{else}}[none]{{end}} Cake is: {{if .Cake}}{{range .Cake}}{{.}} {{end}}{{else}}[none]{{end}} Opt is: {{if .Opt.Exists}}{{.Opt.Value}}{{else}}[none]{{end}} ` func main() { var t Testy json.Unmarshal([]byte(TEXT), &t) tmpl, err := template.New("mytemplate").Parse(TEMPLATE) if err != nil { log.Fatal(err) } tmpl.Execute(os.Stdout, t) b, err := json.Marshal(t) if err != nil { log.Fatal(err) } fmt.Println(string(b)) }