Skip to content

JSON to Go Struct Converter - Generate Go Types

Input JSON
Go Output

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

Frequently Asked Questions

Why int64 and not int?
JSON doesn't say how big a number can get, and IDs from databases and payment APIs regularly go past 2^31. `int64` is the same size as `int` on 64-bit platforms and is explicit about it. If a field is a small count, change it to `int`.
When does a field get a pointer?
Only when the sample has a null for it next to a real value, so `"gift_note": null` becomes `*string`. A pointer is the usual way in Go to tell 'null' from the zero value. Keys that are just missing get `omitempty` and stay plain values. Slices and maps are never wrapped because they can already be nil.
Does omitempty change how decoding works?
No. `omitempty` only affects `json.Marshal`: it drops the field when it holds the zero value (false, 0, "", nil). The catch is that a real `false` is dropped too. If a downstream service needs to see `"backorder": false`, remove `omitempty` or make the field `*bool`. On Go 1.24 and later, `omitzero` is another option.
Can I get nested anonymous structs instead of named types?
Not at the moment. Every object becomes a top-level named type, so you can reuse `Item` in function signatures and tests. Identical shapes under the same key are emitted once.
What do I get for mixed or unknown values?
`any`. That covers fields that are only ever null, empty arrays, and keys whose values change type across records. Swap in `json.RawMessage` if you want to defer decoding, or a custom type with an `UnmarshalJSON` method.