Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: Unmarshal doesn't use preferred match #47984

Closed
zigo101 opened this issue Aug 26, 2021 · 3 comments
Closed

encoding/json: Unmarshal doesn't use preferred match #47984

zigo101 opened this issue Aug 26, 2021 · 3 comments

Comments

@zigo101
Copy link

zigo101 commented Aug 26, 2021

What version of Go are you using (go version)?

$ go version
go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

import "encoding/json"
import "fmt"

type A struct {
	Bar string `json:"Bar"`
	//BAR string `json:"bar"`
}

var s = []byte(`{
	"Bar": "1",
	"bar": "2"
}`)

func main() {
	var a A
	_ = json.Unmarshal(s, &a)
	fmt.Println(a.Bar) // 2
}

What did you expect to see?

prints 1

What did you see instead?

prints 2

@imumesh18
Copy link

It’s because json unmarshlar is case insensitive it treats b and B as same. It’s already documented.

@codehakase
Copy link

Unmarshalling is case insensitive as described in 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.
https://pkg.go.dev/encoding/json#Unmarshal

@seankhliao
Copy link
Member

Duplicate of #14750

@seankhliao seankhliao marked this as a duplicate of #14750 Aug 26, 2021
@golang golang locked and limited conversation to collaborators Aug 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants