Standard library / encoding/json

encoding/json.Decoder.DisallowUnknownFields()

Reject object keys that do not match exported non-ignored fields when decoding into a struct. Does not reject duplicate keys. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (dec *Decoder) DisallowUnknownFields()

Working Example

Example Snippetgo
package main
import (  "encoding/json"  "fmt"  "strings")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  d := json.NewDecoder(strings.NewReader(`{"other":1}`))  d.DisallowUnknownFields()  var x struct{ Count int }  fmt.Println(d.Decode(&x) != nil)}
Go Reference