Skip to content

encoding/json: clarify what happens when multiple items match the same field #43664

Open
@robpike

Description

@robpike

This test I added to encoding/json

func TestUnmarshalCase(t *testing.T) {
	var v struct {
		Dist string `json:"dist"`
	}

	Unmarshal([]byte(`{"dist": "ok", "Dist": "KO"}`), &v)
	t.Logf("%+v", v)

	Unmarshal([]byte(`{"Dist": "ok", "dist": "KO"}`), &v)
	t.Logf("%+v", v)
}

prints

    x_test.go:13: {Dist:KO}
    x_test.go:16: {Dist:KO}

The comment says Unmarshal "prefers" exact matches, but if there are no words about what happens if multiple fields match.

It's clearly an odd case, but clarity would be nice. I expected the exact match to win always.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions