Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering Difference in JSON Marshaled from Map and Struct

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?

like image 829
Pandemonium Avatar asked Apr 18 '16 15:04

Pandemonium


2 Answers

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.

like image 70
Bayta Darell Avatar answered Oct 22 '22 01:10

Bayta Darell


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.

like image 44
Grzegorz Żur Avatar answered Oct 22 '22 02:10

Grzegorz Żur