Skip to content
gohacks.dev
Tooling

JSON to Go Struct

Generate Go structs with json tags from a JSON sample, with nested types, pointers for nullable fields and Go-style initialisms.

Free · No sign-up · Your inputs stay in your browser

Input

Generate Go structs from JSON

Output

Go

478 characters

Whole numbers become int64 and fractions float64; a nullable value becomes a pointer, so encoding/json can tell null from zero. Mixed-type arrays become []any. Initialisms follow Go style (ID, URL).

Runs in this browser. Nothing you paste is sent to a server.

Why pointers for nullable fields

Go has no null for an int or a string: the zero value is 0 or "". A pointer field is nil when the JSON had null or omitted the key, so the two cases can be told apart from a real zero. The encoding/json documentation describes how tags, omitempty and pointers interact.

Numbers

Decoding into any turns every JSON number into float64, which loses precision above 253. Typed fields avoid that. For IDs that may exceed it, the field tag json:",string" reads a quoted number, and json.Decoder.UseNumber keeps numbers as text.

Share

Frequently asked questions

Why are some fields named ID instead of Id?
Go's style guide writes initialisms in a consistent case, so the generator turns Id into ID and Url into URL. The json tag keeps the original key.
What does omitempty do?
When encoding, it leaves the field out if it holds its zero value: 0, "", false, nil or an empty slice or map. It has no effect on decoding.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools