JSON to Go Struct Converter
Convert JSON to Go struct definitions with json tags: exported names with initialisms, int vs float64 detection, omitempty, nested or inline structs.
Turn a JSON sample into Go struct definitions with json tags, ready for encoding/json. Keys become exported Go names with the standard initialisms (id → ID, avatar_url → AvatarURL), numbers become int or float64 depending on the values seen, sometimes-null fields become pointers, and fields missing from some array items get omitempty. The output uses aligned fields; run gofmt after pasting it.
How to use this tool
- Paste a representative JSON sample; arrays with several items give the best types.
- Set the name of the top-level struct.
- Choose separate named types or inline anonymous structs for nested objects.
- Copy the code into your package and adjust any names or types you want to change.
Formula
string → string · whole numbers → int (int64 if beyond 32 bits) · decimals → float64 · true/false → bool · [ … ] → []T · { … } → struct · null or mixed → any
Values found at the same place are merged across array items. If a number is a decimal in any item, the field becomes float64. A field that is null in some items becomes a pointer (*string); one that is null in every item, or holds different kinds of value, becomes any. Each field gets a tag such as json:"avatar_url" so encoding/json maps it back to the original key. Duplicate keys and numbers outside the supported finite, safe-integer range are rejected; other decimals use JavaScript precision.
Example
The sample record gives three types. Root has ID int, Name string, Email string, Active bool, Score float64, Tags []string, Address Address, Orders []Order and Manager any. Address has three string fields. Order has ID string, Total float64 (25.5 and 12 are merged to float64), Items int and Coupon string with json:"coupon,omitempty", because only one order has a coupon.
Frequently asked questions
Why int and not float64 for my number?
Every value seen was a whole number. JSON itself has one number type, and 12.0 is read as 12, so if the field can hold decimals, change it to float64 by hand.
Why are some fields pointers?
The field was null in at least one sample. A pointer lets you tell a JSON null (nil) apart from a zero value such as an empty string.
Should I use inline or separate structs?
Separate named types are easier to reuse, test and document. Inline structs keep a one-off response in a single declaration.
What does omitempty do?
When marshalling, encoding/json leaves the field out if it holds its zero value (empty string, 0, false, nil). It has no effect when unmarshalling. It does not distinguish a missing field from an explicit zero value; use pointers or custom types if that distinction matters.
Sources
Last updated: 2026-10-30