Go structs from a JSON sample
Paste a response body and get structs that encoding/json decodes into without a field left unset. Keys become exported PascalCase field names, each with a json:"key" tag pointing back at the original.
Example
{ "id": 7, "name": "Ada", "orders": [{ "sku": "X1", "qty": 2 }, { "sku": "X2", "qty": 1, "gift": true }] }
type Customer struct {
ID int64 `json:"id"`
Name string `json:"name"`
Orders []Order `json:"orders"`
}
type Order struct {
Sku string `json:"sku"`
Qty int64 `json:"qty"`
Gift *bool `json:"gift,omitempty"`
}
Gift is a pointer because only one order carries it; omitempty on it is added when the omitempty option is checked. ID follows Go’s initialism convention.
Options
Package sets the package line. Types only strips the helper functions. Field tags takes a comma-separated list such as json,yaml or json,db when the same struct is read by more than one encoder. omitempty appends ,omitempty to every optional field’s tag.
Limits
Go has no union type, so a field that mixes strings and numbers in the sample becomes a small struct with one pointer member per type and its own UnmarshalJSON/MarshalJSON. A field that is always null becomes interface{}. Repeated string values become a string-typed Status type with constants when enum inference is on. ISO 8601 strings become time.Time when Dates from ISO strings is on, and the file imports time.