JSON to Go struct

Paste JSON and get Go structs for it below, as you type: exported fields with json tags, lined up as gofmt writes them, ready for json.Unmarshal.

Fields and tags

Each key becomes an exported field in CamelCase, with common initialisms in capitals as Go linters expect (UserID, ImageURL), and a json tag with the key as written. A key some items lack gets omitempty, and a value that may be missing or null is a pointer (*string, *Address), so nil tells it apart from an empty value.

Numbers

Whole numbers that fit in 32 bits are int, larger ones int64, and anything past 64 bits a *big.Int, with its import. A number written with a decimal point, such as 19.0, is a float64 even when it is whole.

Types from sample data

Every item of a list, and every value of JSON Lines, is a sample of the same type: a key that some lack is optional, a value that is sometimes null is nullable, whole numbers mixed with decimals are decimals, and objects with the same keys share one type, named after the key they are under ("line_items" holds LineItem). An object whose keys are ids or dates, such as {"1001": {…}, "1002": {…}}, is a map. YAML, TOML, CSV and the other formats work too: pick their language above the editor.

Or the data as a map literal

Data writes the JSON as a Go value, map[string]any and []any literals with nil for null, lined up the way gofmt does: a fixture to paste into a test.

Copy, download or switch

The Go follows your typing, and a mistake in the JSON is pointed out first, with its line and column. Copy or Download (a .go file) take it as it is. Above the code, switch between Types and Data, or pick another language; the Convert menu's Code item opens the one you picked last.

Private: nothing is uploaded

Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.

With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.

Questions

Why are some fields pointers?

The JSON has null there, or some items leave the key out. A pointer is nil when the value isn't there; a plain string or int would be "" or 0, which can't be told from a real empty string or zero.

Is a package line written?

No: the structs go into a file of yours, under its package. An import comes only when a type needs one (math/big).

Is my data uploaded anywhere?

Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.

More tools