Golang Ordered Map is a map
data structure that maintains the order of the keys.
It also supports JSON and YAML marshalling.
go get github.com/nicolasparada/go-ordered-map
package main
import (
orderedmap "github.com/nicolasparada/go-ordered-map"
)
func main() {
data := []byte(`{ "name": "John", "age": 30, "active": true }`)
var unordered map[string]any{}
if err := json.Unmarshal(data, &unordered); err != nil {
panic(err)
}
var ordered orderedmap.OrderedMap[string, any]
if err := json.Unmarshal(data, &ordered); err != nil {
panic(err)
}
json.NewEncoder(os.Stdout).Encode(unordered) // will print in undefined order
json.NewEncoder(os.Stdout).Encode(ordered) // will always print: {"name":"John","age":30,"active":true}
}