JSON to Go Struct Converter - Generate Go Types
Structs for encoding/json
This writes the struct definitions you'd otherwise type out before calling json.Unmarshal. Every
field is exported, so encoding/json can set it, and the original key is kept in the json tag.
Columns are padded the way gofmt would pad them, so a paste into your editor shouldn't produce a
formatting diff.
Example: an order with line items
Input, with the root type name set to Order:
{
"order_id": 9007199254740993,
"status": "shipped",
"total": 129.95,
"shipping_url": "https://track.example.com/abc",
"items": [
{ "sku": "KB-01", "quantity": 1, "gift_note": null },
{ "sku": "MS-02", "quantity": 2, "gift_note": "Happy birthday", "backorder": true }
]
} Output:
type Order struct {
OrderID int64 `json:"order_id"`
Status string `json:"status"`
Total float64 `json:"total"`
ShippingURL string `json:"shipping_url"`
Items []Item `json:"items"`
}
type Item struct {
Sku string `json:"sku"`
Quantity int64 `json:"quantity"`
GiftNote *string `json:"gift_note"`
Backorder bool `json:"backorder,omitempty"`
} order_id and shipping_url follow Go's initialism style and become
OrderID and ShippingURL. The tool knows a short list (ID, URL, URI, API, HTTP, JSON,
XML, HTML, UUID, IP, SQL, CSS, UI), so sku stays Sku; rename it to SKU if
your linter complains. The items slice gives an Item struct, named from the singular
of the key.
Large IDs
9007199254740993 is 2^53 + 1, the first integer a float64 can't hold. Decoded into the
int64 field above, Go keeps it exact. Decoded into map[string]any, it becomes a
float64 and silently turns into 9007199254740992. That is the main reason to decode
into structs instead of maps. If you must use any, call dec.UseNumber() on a
json.Decoder to get json.Number strings instead.
Things to adjust
Timestamps come out as string. If they are RFC 3339, such as
"2026-11-10T09:00:00Z", change the field to time.Time and encoding/json parses it for
you. Money in float64 is fine for display but not for arithmetic; consider an integer of cents. To
reject payloads with keys your struct doesn't declare, decode with json.NewDecoder(r) and call
DisallowUnknownFields() first.
var order Order
if err := json.Unmarshal(body, &order); err != nil {
return fmt.Errorf("decode order: %w", err)
}
Keys that start with a digit get an N prefix, and two keys that map to the same Go name (say
a and A) become A and A2 so the struct compiles. Need a
contract that other languages can check too? Try JSON to JSON Schema.
Same JSON, other languages
Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: TypeScript, Zod, Python, Rust, Java, C#, JSON Schema.
Related Tools
JSON to YAML Converter
Convert JSON data to clean, readable YAML format instantly.
JSON to XML Converter
Transform JSON objects and arrays into well-formed XML documents.
JSON to CSV Converter
Flatten JSON arrays into downloadable CSV spreadsheets in one click.
YAML to JSON Converter
Convert YAML configuration files to JSON format instantly.