type Profile struct {
Age int `json:"age"`
City string `json:"city"`
}
type AutoGenerated struct {
ID int `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
Score float64 `json:"score"`
Tags []string `json:"tags"`
Profile *Profile `json:"profile"`
}About JSON to Go Struct
Go is strongly typed, so consuming a JSON API means defining matching structs first — slow and easy to mistype json tags. This tool parses your sample JSON locally in the browser and generates Go structs with json tags: field names become exported PascalCase, nested objects split into their own structs, arrays infer to slice types, with an optional omitempty switch. Turning an API response into Go structs is the first step when integrating a service. Go's strong typing and explicit null handling mean the definition must be written accurately, or problems appear at compile time or runtime: using a value type where a pointer belongs discards the distinction between present and absent, and the wrong integer width overflows large values. Generation parses the JSON, infers field types and maps names to exported fields with tags. A few decisions need human review: nullability cannot be inferred from one sample and needs several covering missing and null fields, expressed with pointers or a nullable wrapper. On numbers, integers and floats must be separated, and values beyond the safe range plus monetary amounts should use strings or fixed-point types to avoid precision loss. Whether a time field is a string or an instant type depends on whether the API includes an offset. Practical notes: Mind the export rule when mapping names, since Go can only serialize exported fields, meaning the first letter must be uppercase, while a tag carries the original key. Both are required, with a missing tag producing the silent failure where every field holds its zero value. Nested structures are better split into named types than inlined anonymous ones, for reuse and method attachment, and array element types should be checked for possible mixing, which needs a wider representation in a strongly typed language. Limits: Type inference cannot express business constraints such as enumerations, required fields or value ranges, which need validation tags or later checks. Generated code is a starting point and should not enter production unreviewed, especially when the sample contains debug fields that would then be frozen into the contract. Go also cannot distinguish a zero value from an absent field, so where that distinction matters the design must use pointers or an explicit presence flag, and that should be settled after generation rather than discovered at runtime. Data and privacy: samples used for generation often contain real user data and tokens. This tool infers and generates in the browser without uploading. Replace tokens and personal identifiers before generating from a production response. Misuse clusters around nulls and numeric types: an all-populated sample cannot reveal nullability, so every field becomes a value type and a null silently deserialises to the zero value. And putting money on a float introduces rounding at the cent level. Go also only serialises exported fields, so a lower-case field with a tag produces the silent failure where everything holds its zero value.
How It Works Internally
Generation maps the JSON structure onto structs with tags. Go export rules are a hard constraint: only fields with an uppercase first letter can be written by the serialiser, and a tag must carry the original key, both being required — a tag on an unexported field produces the silent failure where every value is the zero value. Nullability, numeric types and time representation all need review.
{"user_id":1}type Root struct { UserID int `json:"user_id"` } (the field must be exported and the tag is mandatory)Measured on This Page
| Fixed: duplicate fields | All three keys id, ID and Id Pascal-case to ID, and the old code emitted three ID fields — Go rejects that as a duplicate field. Suffixes now follow appearance order: ID / ID2 / ID3 |
|---|---|
| Fixed: CJK keys all named Field | pascal keeps only [A-Za-z0-9], so a CJK key collapses to the fallback Field: {"用户":1,"订单":2} produced two Field fields that could never compile. They are now Field / Field2 |
| Nesting and slices | Nested objects become their own struct behind a pointer field, string arrays become []string, and a mixed array falls back to []interface{} |
| Big integers become string | With the big-integer guard the value arrives as a JSON string, so the Go type is string — switch to int64 by hand where needed (noted) |
| null | null maps to interface{}, matching Go's zero-value semantics |
A generator earns trust by producing something that compiles, and both of the 2 defects found here broke exactly that: Pascal-casing collided (id, ID, Id converging on ID) and non-ASCII keys collapsed onto one fallback name, each yielding duplicate field errors from the compiler. Numbered suffixes in appearance order resolve both without inventing meanings. Two limits worth knowing: a 16-digit number lands as a quoted Go string (the editor refuses to fake its digits), and a heterogeneous array degrades to []interface{} — normalise the payload upstream when firm types matter.
How to Use
- Open "JSON to Go Struct"
- Paste one sample JSON document
- Name the root struct
- Toggle omitempty on the JSON tags
- Click the copy icon to take the generated code
Use Cases
- Consume REST APIs — Paste the JSON an API returns and instantly get a Go struct ready to Unmarshal into.
- Define config structs — Generate a struct from a config sample to load YAML/JSON configuration.
- Build scrapers/collectors — Model third-party responses quickly and skip hand-writing fields.
- Bootstrap gRPC/messages — Reverse-engineer a structure from sample data as a starting point for messages or DTOs.
- Learn Go type mapping — See at a glance which Go types map to each JSON type to aid learning.
- Model AWS SDK payloads — Paste an AWS SDK response JSON and generate Go structs for typed unmarshaling in Lambda functions.
- Generate API client types — Create Go structs from a REST API sample to build a typed client library without manual mapping.
FAQ
How are field names converted?
They become exported PascalCase per Go conventions, with common initialisms like Id/URL/API uppercased, while keeping the original json tag.
What does a null value infer to?
null infers to interface{} since the concrete type is unknown. Use a non-null sample for a more precise type.
What is omitempty for?
When enabled it appends ,omitempty to the json tag so empty fields are omitted on marshal — handy when many fields are optional.
Do I need to adjust the output?
It is standard Go code you can rename, retype or comment after copying. Integers default to int and decimals to float64; change to int64 etc. as needed.
Are time/date fields handled specially?
By default they are generated as string fields. If an API field uses a standard time format, switch it to time.Time with a declared format in the json tag and encoding/json parses it automatically.
What about empty or null values?
Sparse or null samples infer to interface{} to avoid compile errors. For precise types, provide a sample with representative non-null values.
Can I lowercase the generated struct fields?
Go requires exported fields for encoding/json, hence the capitals. Lower-case fields work with a json tag, but the tool emits the exported form by default. If you lowercase them, add json:"camelCase" tags or serialisation changes.
Numbers come out as float64 — can I get int?
Values like 1.0 or 1.5 force float64, and mixed int/float fields too. Change confirmed-integer fields to int/int64 by hand. Note that decoding into interface{} yields float64 in Go anyway, so this is not a tool limitation.