JSON to Go Struct Generator

Paste JSON and get Go struct definitions with json tags, generated locally in your browser.

Convert JSON to Go structs

Paste a JSON document and click Generate to get Go type definitions you can drop straight into a package. Each object becomes an exported struct, each key becomes an exported field in PascalCase, and a `json:"..."` tag keeps the original key so encoding/json can Unmarshal the data directly. The fields are aligned the way gofmt aligns them.

How to use the JSON to Go converter

  1. Paste a JSON object or array into Input. Several records in an array give better results than one, because optional and nullable keys only show up across records.
  2. Click Generate. The structs appear under Generated code, starting with package main.
  3. Click Copy, paste into a .go file, change the package name and run gofmt.
  4. Decode with json.Unmarshal(data, &root) and adjust any types the sample could not pin down.

How fields are typed

JSONGo
whole numberint64
decimal, or mixed whole and decimalfloat64
string / booleanstring / bool
objectNamed struct type
arraySlice such as []string or []Order
sometimes nullPointer such as *float64, so nil is distinct from 0
missing from some array itemsTag gets ,omitempty
only ever nullinterface{}

Example

{ "items": [
  { "id": 1, "note": "a", "score": null },
  { "id": 2, "score": 3.5, "created_at": "2026-01-01" } ] }

becomes:

type Root struct {
	Items []Item `json:"items"`
}

type Item struct {
	Id        int64    `json:"id"`
	Note      string   `json:"note,omitempty"`
	Score     *float64 `json:"score"`
	CreatedAt string   `json:"created_at,omitempty"`
}

Then var r Root; err := json.Unmarshal(data, &r). The array items was merged across both elements and named with its singular, Item.

Good to know

More examples

Top-level arrays and awkward keys

[{"sku":"A1","qty":2,"user-id":5,"2fa":true}]

generates

package main

// The top-level value is an array; the classes below describe its elements.
type Root struct {
	Sku    string `json:"sku"`
	Qty    int64  `json:"qty"`
	UserId int64  `json:"user-id"`
	N2fa   bool   `json:"2fa"`
}

The comment marks that the JSON root is an array, so decode into []Root. Keys with dashes keep their exact spelling in the tag, and a key starting with a digit gets an N prefix to form a legal Go identifier.

Numbers and mixed arrays

{"id":12345678901234567890,"ratio":1.0,"values":[1,"a"],"tags":[]}

generates

package main

type Root struct {
	Id     int64         `json:"id"`
	Ratio  int64         `json:"ratio"`
	Values []interface{} `json:"values"`
	Tags   []interface{} `json:"tags"`
}

Three things to fix by hand here. The ID is beyond the int64 range, so Unmarshal would fail; use uint64 or a string. 1.0 reaches the generator as the whole number 1, so ratio is typed int64, and a later value of 1.5 would be rejected with cannot unmarshal number 1.5 into Go struct field; change it to float64. The empty and mixed arrays become []interface{} (the same as []any since Go 1.18).

Common errors after generating

Go structs or another output?

Use this page when Go code consumes the JSON. For a contract shared with other languages, start with the JSON Schema generator; for the same payload on the frontend, use JSON to TypeScript; for .NET, JSON to C#. If the input will not parse, the JSON validator shows the exact line and column of the error.

FAQ

Why are some fields pointers?

The sample showed null for that key in at least one place. A pointer lets you tell a JSON null (nil) apart from a zero value.

Does it produce nested anonymous structs?

No. Every nested object gets its own named type, which is easier to reuse and test than inline struct { ... } fields.

Why does it write Id instead of ID?

Field names are built by capitalising each word of the key, so id becomes Id and user_url becomes UserUrl. Rename them to ID and UserURL if golint asks; the json tag keeps the mapping correct.

Why is a field typed interface{}?

The sample did not reveal a single type: the value was always null, the array was empty, or the array mixed types. Replace it with a concrete type, or json.RawMessage if you want to decode it later.

How do I handle numbers bigger than int64?

Values above 9223372036854775807 overflow int64 and Unmarshal returns an error. Use uint64 if they are never negative, or json.Number or a string if they can be anything.

Is my JSON sent anywhere?

No. Parsing and code generation run in your browser.