Skip to content

encoding/json: unexpected json.Unmarshal behavior with maps #12972

@nsoufr

Description

@nsoufr

http://golang.org/src/encoding/json/decode.go?s=2621:2669#L64

To unmarshal a JSON object into a map, Unmarshal replaces the map with an empty map and then adds key-value pairs from the object to the map.

But, while unmarshalling it behaves differently.

http://play.golang.org/p/HTjkkCEQBV

var r map[string]string
j := []byte(`{"hello": "world"}`)

json.Unmarshal(j, &r)
fmt.Println(r)
// map[string]string{"hello":"world"}

ja := []byte(`{"world": "hello"}`)
json.Unmarshal(j, &r)
fmt.Println(r)
// map[string]string{"hello":"world", "world":"hello"}
// expected to eq map[string]string{"world": "hello"}

I'm totally in favor of this behavior but what docs is saying isn't correct.

That make sense?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions