About This Tool
JSON → Go Struct generates named structs and slice types for Go 1.18+. Field names become valid exported identifiers, and json tags preserve original keys where possible. Check encoding/json restrictions for complex or unusual keys.
How to Use It
- Enter JSON. Multiple object samples help identify missing fields.
- Set the root struct name.
- Click Convert and review exported fields, json tags, nullable pointers, and slice types.
- Copy or download the Go source, then adapt it to your package and project requirements.
Examples
- {"first_name":"Tom"}: generate exported field FirstName with json:"first_name".
- [{"id":1,"name":"Tom"},{"id":2}]: name is optional and may use *string with omitempty.
- {"matrix":[[1,2],[3,4]]}: infer [][]int64.
Type Mapping Rules
Strings use string, safe integers int64, decimals or mixed numbers float64, booleans bool, and arrays []T. Known nullable or optional types use suitable pointers; missing fields get omitempty. Unknown, null-only, or incompatible mixed values use any. The target is Go 1.18+. Fields are exported, struct names are stable and conflict-free, and tags use valid Go string escaping. encoding/json gives special meanings to commas, hyphens, and some control-character keys. Keys that cannot round-trip directly are called out in the output; valid Go syntax does not guarantee automatic mapping for every JSON key. Input and output are each limited to 1 MiB, with up to 64 nesting levels, 100,000 nodes, 5,000 merged fields, and 256 types. Unsafe integers, nonfinite numbers, and numbers that underflow to zero produce an error. Other decimals use the browser’s floating-point precision.
Common Uses
- Draft Go data types from service response samples.
- Review pointer requirements for nullable and optional fields.
- Create named structs and slices for nested JSON.
Privacy
JSON inference and Go code generation run entirely in your browser, without sending data to a server or online generator.
If you need to keep the results, copy or download them before leaving the page.
FAQ
Why do field names start with an uppercase letter?
Go’s encoding/json generally needs exported fields. An uppercase initial makes a field accessible outside its package and to the standard library.
What does a json tag do?
It maps a struct field to its JSON key, preserving common original keys after field renaming.
How is null handled?
A known type combined with null can use a pointer. When null is the only evidence, a conservative type such as any is used.
Why use any?
The target is Go 1.18+. any is an alias for interface{} and suits types that the sample cannot establish.
Can every JSON key be used directly in a tag?
Not necessarily. Go tag strings support escaping, but encoding/json also interprets special content such as commas and hyphens. Such keys may need custom mapping; review the generated notes.