Developer Tools

JSON to Go Struct

Generate Go struct definitions with json tags from a JSON sample, including nested types, slices, optional and nullable fields.

Struct tags
Go
package main

type User struct {
	ID        int      `json:"id"`
	UserName  string   `json:"user_name"`
	AvatarURL any      `json:"avatar_url"`
	Verified  bool     `json:"verified"`
	Profile   Profile  `json:"profile"`
	Roles     []string `json:"roles"`
	Orders    []Order  `json:"orders"`
}

type Profile struct {
	DisplayName   string `json:"display_name"`
	FollowerCount int64  `json:"follower_count"`
}

type Order struct {
	ID         string  `json:"id"`
	Total      float64 `json:"total"`
	Items      []Item  `json:"items"`
	CouponCode string  `json:"coupon_code,omitempty"`
}

type Item struct {
	Sku string `json:"sku"`
	Qty int    `json:"qty"`
}

Types are inferred from this sample. Whole numbers become int (int64 when too large for 32 bits), decimals float64, and values that are null everywhere become any.

About this tool

Decoding an API response in Go starts with a struct whose fields and tags match the JSON exactly. Writing it by hand means converting every key to an exported Go name, guessing number types and repeating the original key in a tag. Paste a JSON sample here and you get gofmt-formatted Go types with json tags, one named struct for every nested object.

The generator follows Go conventions: user_id becomes UserID and avatarUrl becomes AvatarURL, using the same initialisms as Go linters. When array items differ, keys that only some items have get omitempty, values that are sometimes null become pointers, and integers too large for 32 bits become int64. Everything runs in your browser.

How to use JSON to Go Struct

  1. Paste a JSON sample or open a .json file.
  2. Set the struct name and package, and choose the tags to emit (json, yaml, mapstructure).
  3. Choose whether to use omitempty, pointers for nullable values, and separate or inline nested structs.
  4. Copy the code or download it as types.go.

Examples

Go naming conventions

Input
{"user_id": 7, "avatar_url": "https://…", "api_key": "k"}
Result
UserID int `json:"user_id"`; AvatarURL string; APIKey string

Items that differ

Input
{"items": [{"sku": "A", "note": null}, {"sku": "B", "note": "gift", "coupon": "X"}]}
Result
type Item struct { Sku string; Note *string `json:"note,omitempty"`; Coupon string `json:"coupon,omitempty"` }

Number types

Input
{"id": 9007199254740991, "ratio": 0.5, "count": 3}
Result
ID int64; Ratio float64; Count int

How types are chosen

  • Whole numbers become int, or int64 when a value does not fit in 32 bits; numbers with decimals become float64. If an array mixes both, float64 is used.
  • Nested objects become named structs; their names come from the key, singularised for array items (orders → Order).
  • A value that is null in some places and a string in others becomes *string, so you can tell “missing” from the empty string. A key that is null everywhere becomes any.
  • Arrays of mixed types and empty arrays become []any.
  • Two objects with exactly the same shape share one struct.

Things to check before using the structs

A single sample cannot show every case. Dates arrive as strings: change the type to time.Time if they are RFC 3339. Large IDs from JavaScript APIs are often sent as strings for precision; keep them as strings or add the ,string tag option. If a number can have decimals but your sample happens to show whole numbers, change int to float64 by hand.

Frequently asked questions

Everything you need to know about this tool and data security.

Is my JSON uploaded?

No. Parsing and code generation happen in your browser, so responses containing personal data or tokens stay on your device.

Why are some fields pointers?

Because they are null in at least one place in your sample. A pointer lets your code tell a missing or null value from a zero value. Turn off “Pointers for nullable values” if you prefer plain types.

What does omitempty do?

When encoding, encoding/json leaves out fields with zero values. The tool adds it only to keys that are missing or null somewhere in your sample.

Should nested structs be separate types or inline?

Separate named types are easier to reuse and to reference in functions. Inline anonymous structs keep everything in one declaration, which suits one-off decoding.

Does it use any or interface{}?

any, which is an alias for interface{} available since Go 1.18.

Is my data safe when using this tool?

Yes, 100% safe. This tool operates entirely inside your web browser using client-side JavaScript and WebAssembly. Your data never touches any external server.