GotoStruct
Open source Go package designed to help you converting JSON objects into Go Structs.
Examples
List of examples can be found within GotoStruct Examples.
Following code snippet:
package main
import (
"fmt"
gs "github.com/0x19/gotostruct"
"strings"
)
func main() {
gts := gs.GotoStruct{
Name: "Example - Struct",
}
reader := strings.NewReader(`{"simple" : "json", "test": null, "something": [{"baz": 1}, {"baz": 2}]}`)
response, err := gts.Generate(reader)
if err != nil {
fmt.Errorf("Got Error while generating struct: ", err)
}
fmt.Println(string(response))
}
will produce following struct:
type ExampleStruct struct {
Simple string `json:"simple"`
Something []struct {
Baz float64 `json:"baz"`
} `json:"something"`
Test interface{} `json:"test"`
}
Related Work
Parts of GotoStruct code are taken from Json-to-struct.
Contributing
I encourage you to contribute to GotoStruct! Please check out the Contributing to GotoStruct for guidelines about how to proceed.