Package json2go provides utilities for creating well formatted go type representation from json inputs.
Simple web page with webassembly json2go parser
https://m-zajac.github.io/json2go
go get github.com/m-zajac/json2go/...
Json2go can be used as cli tool or as package.
CLI tools can be used directly to create go type from stdin data (see examples).
Package provides Parser, which can consume multiple jsons and outputs go type fitting all inputs (see examples and documentation). Example usage: read documents from document-oriented database and feed them too parser for go struct.
echo '{"x":1,"y":2}' | json2go
curl -s https://api.punkapi.com/v2/beers?page=1&per_page=5 | json2go
Check this one :)
cat data.json | json2go
inputs := []string{
`{"x": 123, "y": "test", "z": false}`,
`{"a": 123, "x": 12.3, "y": true}`,
}
parser := json2go.NewJSONParser("Document")
for _, in := range inputs {
parser.FeedBytes([]byte(in))
}
res := parser.String()
fmt.Println(res)
{
"line": {
"point1": {
"x": 12.1,
"y": 2
},
"point2": {
"x": 12.1,
"y": 2
}
}
}
type Document struct {
Line struct {
Point1 Point `json:"point1"`
Point2 Point `json:"point2"`
} `json:"line"`
}
type Point struct {
X float64 `json:"x"`
Y int `json:"y"`
}
[
{
"name": "water",
"type": "liquid",
"boiling_point": {
"units": "C",
"value": 100
}
},
{
"name": "oxygen",
"type": "gas",
"density": {
"units": "g/L",
"value": 1.429
}
},
{
"name": "carbon monoxide",
"type": "gas",
"dangerous": true,
"boiling_point": {
"units": "C",
"value": -191.5
},
"density": {
"units": "kg/m3",
"value": 789
}
}
]
type Document []struct {
BoilingPoint *UnitsValue `json:"boiling_point,omitempty"`
Dangerous *bool `json:"dangerous,omitempty"`
Density *UnitsValue `json:"density,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
}
type UnitsValue struct {
Units string `json:"units"`
Value float64 `json:"value"`
}