After debugging an issue for a while, I realized that the JSON tags in Golang structs are case-insensitive. Below is a code sample that demonstrates this.
package main
import (
"fmt"
"encoding/json"
)
type Person struct {
Name string `json:"Name"`
}
var stringifiedJSON = `{"Name": "mallikarjuna", "name": "abhilash"}`
func main() {
var person Person
if err := json.Unmarshal([]byte(stringifiedJSON), &person); err != nil {
fmt.Println(err)
return
}
fmt.Println(person.Name) // abhilash
}
Go playground link
Explanation from the docs:
To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match.
Read entire docs here.