You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var e Example
err := json.Unmarshal([]byte(jsonData), &e)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", e.Name) // Expected: "Alice", Actual: "Charlie"
}`
What did you see happen?
Instead of selecting the first exact match (i.e., "name": "Alice"), json.Unmarshal took the last occurrence of the key, "Name": "Charlie", regardless of case.
Result: Charlie
What did you expect to see?
The Go documentation suggests that json.Unmarshal prefers exact matches first before falling back to case-insensitive matching.
I expected the output to be: Result: Alice
since "name" is an exact match to the struct tag.
Instead, Go appears to always take the last occurrence in JSON, regardless of case.
The text was updated successfully, but these errors were encountered:
Go version
go1.21.4 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
I ran the following Go program to test how json.Unmarshal handles case-insensitive JSON keys:
`package main
import (
"encoding/json"
"fmt"
)
type Example struct {
Name string
json:"name"
}
func main() {
jsonData :=
{"name": "Alice", "NAME": "Bob", "Name": "Charlie"}
}`
What did you see happen?
Instead of selecting the first exact match (i.e., "name": "Alice"), json.Unmarshal took the last occurrence of the key, "Name": "Charlie", regardless of case.
Result: Charlie
What did you expect to see?
The Go documentation suggests that json.Unmarshal prefers exact matches first before falling back to case-insensitive matching.
I expected the output to be:
Result: Alice
since "name" is an exact match to the struct tag.
Instead, Go appears to always take the last occurrence in JSON, regardless of case.
The text was updated successfully, but these errors were encountered: