JSON to Go Struct

Generate Go structs from JSON

About JSON to Go Struct

JSON to Go Struct turns a JSON sample into Go type declarations for encoding/json, generated by quicktype-core in your browser. Each nested object becomes its own named struct, field names follow Go style including initialisms (user_id becomes UserID), and every field keeps the original key in a json tag. Whole numbers are int64 and numbers written with a decimal point are float64. When you paste an array, a key that only some elements have becomes a pointer with omitempty. The output holds only the type definitions: no package line, no methods, no marshal helpers.

Example

One object with a nested address: the address gets its own struct.

JSON input

{"user_id": 42, "score": 9.5, "tags": ["go", "api"], "address": {"city": "Montreal", "zip": "H2X"}}

Go struct output

type Root struct {
	UserID  int64    `json:"user_id"`
	Score   float64  `json:"score"`
	Tags    []string `json:"tags"`
	Address Address  `json:"address"`
}

type Address struct {
	City string `json:"city"`
	Zip  string `json:"zip"`
}

How to convert JSON to a Go struct

  1. 1Paste the JSON, for example an API response, into "JSON input".
  2. 2Click "Convert" to build the structs in "Go struct output".
  3. 3Add your own package line above the types, since the tool writes only the declarations.
  4. 4Click "Copy", or "Download" to get the file "model.go".

Formats and limits

Input
JSON text; with an array, the fields of all elements are merged
Output
Go struct declarations with json tags, saved as model.go
Number types
int64 for whole numbers; float64 for numbers written with a point or an exponent, such as 10.0
Optional keys
Pointer type plus omitempty when a key is missing from some elements; pointer alone when it is null in some; interface{} when it is null in every sample
Mixed types
A key that holds a number in one element and text in another becomes a struct with Integer and String pointer fields; encoding/json cannot fill it without an UnmarshalJSON method you write

Where your JSON is processed

Struct generation runs in this browser tab. The quicktype engine is fetched once, on your first click, and your JSON stays on your computer. The page does send error reports: when a red error line appears, its wording, with quoted parts, long numbers, and addresses stripped, goes to our error log along with the tool name and the name and version of your browser, and an unexpected failure of the page is reported the same way.

Frequently asked questions

Is a field a pointer when a key is sometimes missing or null?

Yes. A key missing from at least one element becomes a pointer with omitempty, which leaves it out when encoding; a key that is null in one element becomes a plain pointer, without omitempty. If the key is always set in your real data, change *string to string by hand.

Which number types does it use?

int64 for whole numbers and float64 for decimals. A value written as 10.0 counts as a decimal, so it becomes float64 and a later 10.5 still decodes. Switch to int or int32 yourself if your values are small.

How does it name fields from snake_case keys?

It turns them into exported Go names and keeps the key in the tag: user_id becomes UserID, and its tag still says user_id, so encoding/json reads and writes the original key unchanged.

Can it generate a struct for a JSON array?

Yes. A top-level array gives type Root []RootElement, and the fields of all elements are merged into RootElement, so one element without a key is enough to mark that key optional.

Tips and tricks

  • ✓Paste two or three different items from an API list rather than one, so that keys that are sometimes absent become pointers.