Standard library / encoding/json

JSON duplicate object keys

The Go 1.23 JSON decoder processes duplicate object keys in order, with later values replacing or merging earlier values depending on the destination. Valid and DisallowUnknownFields do not reject duplicates. Expected stdout: '2\n'.

Formal Syntax & Structure

go syntaxgo
{"x":1,"x":2}

Working Example

Example Snippetgo
package main
import (  "encoding/json"  "fmt")
func must(e error) {  if e != nil {    panic(e)  }}
func main() {  var m map[string]int  must(json.Unmarshal([]byte(`{"x":1,"x":2}`), &m))  fmt.Println(m["x"])}
Go Reference