I maintain mapstructure as a Go library for decoding generic map values into native Go structures and vice versa. It is useful when the data’s structure depends on information encountered while reading a stream: inspect a generic map first, then decode into the appropriate structure.
The mapstructure repository is on @mitchellh's GitHub.
The repository has 8,006 stars.
The repository has 750 forks.
The repository started in 2013-05.
The repository was last updated in 2024-06.
mapstructure is a Go library for decoding generic map values to structures and vice versa, while providing helpful error handling.
The README describes the use case this way: “This library is most useful when decoding values from some data stream (JSON, Gob, etc.) where you don't quite know the structure of the underlying data until you read a part of it.”
For this flow, the README says: “You can therefore read a map[string]interface{} and use this library to decode it into the proper underlying native Go structure.”
The README’s discriminator example contains: "type": "person", "name": "Mitchell"
The standard JSON approach populates a pre-created struct, which can be inconvenient when a field in the data determines which structure to use. The README presents inspecting a generic map and then decoding into the selected structure as a simpler alternative to two decoding passes.
Standard go get:
$ go get github.com/mitchellh/mapstructure
The README directs readers to the Godoc for usage and examples: “For usage and examples see the Godoc. The Decode function has examples associated with it there.”