When marshaling from a map[string]interface{}
and an equivalent struct
, the JSON returned are similar but different in the internal order of the keys likewise:
var arg1 = map[string]interface{}{
"foo": "bar",
"baz": map[string]interface{}{
"bee": "boo",
},
}
type Arg struct {
Foo string `json:"foo"`
Baz map[string]interface{} `json:"baz"`
}
var arg2 = &Arg{
Foo: "bar",
Baz: map[string]interface{}{
"bee": "boo",
},
}
func main() {
result1, _ := json.Marshal(arg1)
result2, _ := json.Marshal(arg2)
fmt.Println(reflect.DeepEqual(result1, result2)
fmt.Println(string(result1))
fmt.Println(string(result2))
// false
// {"baz":{"bee":"boo"},"foo":"bar"}
// {"foo":"bar","baz":{"bee":"boo"}}
}
Check the playground here.
Although in JSON the order doesn't matter, I'd like to know why are the results ordered differently?
The encoding/json package marshals maps in sorted key order and structs in the order that the fields are declared.
Although the order used by the encoding/json package is not documented, it's safe to assume that maps are marshaled in sorted key order and structs are marshaled in field declaration order. There are many tests in the standard library and elsewhere that depend on these orders.
The order of elements in maps is not defined as Go uses a hash table to implement them. You should expect the elements to be marshalled in any order.
When marshalling a structure, the order of JSON fields should match the order of fields in structure. This is a side effect of accessing fields through reflection. Still you should not rely on this side effect.
The order of elements is not significant in JSON. The problem of comparing JSON structures is the same as the problem of comparing trees with unordered child nodes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With