Writing a Go struct by hand from a JSON sample means guessing at types, remembering to capitalise every field so it exports, and typing a matching json tag onto each line so the original key still decodes correctly once the Go field name has been converted to the language's PascalCase convention. It is mechanical work that is easy to get slightly wrong, particularly once nesting and arrays of objects are involved.
Everything runs locally using plain JavaScript. Your JSON sample, which is very often a real response from a production API, is parsed and walked entirely in your browser, so pasting a payload containing customer data or internal identifiers never means sending that data anywhere. Whether you think of this as a way to generate go struct from json samples quickly, or simply as a shortcut to convert json to golang types, the underlying walk over your data is the same.
When you should generate a Go struct from a JSON sample
The moment worth reaching for this is whenever you start writing Go code that reads or writes a JSON document you do not already have a struct for, whether that is a third party API response, a webhook payload, or a configuration file loaded at startup. Typing the struct from a written specification is slow and frequently drifts from what the service actually sends, while generating it from a genuine sample matches production reality on the first attempt.
It is also the fastest way to get a compiling starting point when a payload changes shape. Rather than manually editing an existing struct field by field while comparing it against a diff of the new response, regenerating the whole struct from the new sample and merging in any custom logic is usually quicker and less error prone.
- Generate a struct when integrating a new third party API that returns JSON.
- Regenerate a struct whenever the underlying API response shape changes.
- Use it for a JSON configuration file loaded once at program startup.
- Use it to get compile time field name safety instead of working with map[string]interface{}.
How this json to go struct converter infers field types
Every JSON object becomes a named Go struct, with each key mapped to an exported field and each value's type inferred from what was actually present in the sample. A JSON string becomes Go's string, a JSON boolean becomes bool, and a JSON number becomes float64 by default because that mirrors what Go's own encoding/json package does when it decodes a number into an untyped interface, avoiding a struct that would fail to decode a value the sample never showed as a whole number.
Nested objects get their own named struct rather than being flattened, so a top level Order document containing a nested customer object produces both an Order struct and a separate Customer struct that Order references by name, matching the way hand written Go code is normally organised into small composable types.
A JSON value of null is represented as a pointer type so the field can distinguish an explicitly absent value from a zero value, which is the same technique idiomatic Go code uses whenever a JSON field is optional and the difference between missing and empty actually matters to the program.
Why every generated field gets a json struct tag
Go's exported field naming convention requires a capital first letter, but JSON keys are almost always written in camelCase or snake_case, so a field converted straight to Go's PascalCase would no longer match the original key when encoding/json tries to decode it. The json struct tag written under each field, for example `json:"shipping_address"`, tells the decoder exactly which JSON key maps to which Go field regardless of how the field name itself was rewritten.
This converter always writes the original key into the tag exactly as it appeared in your sample, rather than assuming a naming convention, since a struct tag is the one place correctness genuinely matters. A field name can be renamed freely for readability once the tag has locked in the real JSON key it corresponds to.
Handling arrays and mixed field types across a json struct tags generator
Arrays of objects are the trickiest input, because different entries in a real array frequently do not all carry the exact same set of keys. This converter merges every object found inside an array together before generating the struct, so a key present in every entry becomes a plain field and a key missing from at least one entry is still included, since Go has no concept of an optional struct field the way TypeScript does, only a pointer or a zero value standing in for absence.
When a single key holds different primitive types across different array entries, the converter falls back to interface{} for that field rather than guessing incorrectly, because Go's static type system has no union type and forcing one primitive type onto a field that genuinely varies would produce a struct that fails to decode some of the very data it was generated from.
Reviewing the generated Go struct before you commit it
Generated code is a starting point rather than a finished answer. Skim the field names for anything that would read more naturally renamed, since the converter derives names mechanically from JSON keys and does not know your project's own naming conventions. Also check any field that came back as interface{}, since that usually means the sample contained a genuinely mixed type and the calling code will need an explicit type assertion or a type switch to use that value safely.
Once the shape looks right, paste the struct into your project alongside whatever HTTP client or file reader consumes the JSON, and run go vet or a quick unmarshal test against a real payload to confirm the generated tags line up exactly with what the service actually returns. Teams that need to generate a golang struct from json repeatedly, for example whenever a webhook payload changes shape, tend to keep a copy of the original sample checked in alongside the struct so the two can be regenerated and diffed together.
| JSON type | Go type | Note |
|---|---|---|
| string | string | Direct mapping |
| number | float64 | Matches what encoding/json decodes into an untyped interface |
| boolean | bool | Direct mapping |
| null | pointer type | Distinguishes an explicit null from a missing zero value |
| object | named struct | A separate struct type is generated and referenced by name |
| array of objects | []StructName | Every entry is merged so no field observed in the sample is lost |
| mixed type array | interface{} | Used when a single key holds more than one primitive type |
How to convert JSON to a Go struct
- 1
Add your JSON sample
Paste a real JSON object or array into the box, or drop a .json file. Nothing leaves your device.
- 2
Name the root struct
Give the top level struct a descriptive name, such as Order or ApiResponse.
- 3
Review the package name
Set the package declaration at the top of the file, defaulting to main so the output compiles standalone.
- 4
Generate and copy the struct
Press generate, review the field types and tags, then copy the Go code or download it as a .go file.
Related tools worth bookmarking
Sources and further reading
- Go documentation: encoding/jsonThe standard library package that decodes JSON into the structs this tool generates, including struct tag syntax.pkg.go.dev
- The Go Programming Language Specification: Struct typesThe official language specification defining struct type syntax and field declarations.go.dev
- JSON.org: Introducing JSONThe official description of the JSON format this tool reads as input.json.org
Frequently asked questions
Common questions about the json to go struct converter.
This json to go struct converter strips every character that is not a letter or a digit and capitalises what remains, so a key such as first-name becomes FirstName and a key starting with a digit gets a safe fallback. The original key is never lost, because the json struct tag always carries it verbatim, which is what lets the generated struct decode a real payload using that exact key.
Go's exported fields must start with a capital letter, but JSON keys rarely do, so the two names diverge once the field is capitalised. The json struct tag tells encoding/json exactly which original key maps to each field, which is what lets the generated struct actually decode the sample it was built from.
The converter merges every object in the array together so no field is lost, since Go has no built in concept of an optional struct field the way some other languages do. A key that only appears in some entries is still included in the generated struct rather than silently dropped.
That happens when a single key held genuinely different primitive types across different entries in your sample, for example sometimes a number and sometimes a string. Go's type system has no union type, so the safest generated field type is interface{} paired with a comment suggesting a manual type switch where the value is actually used.
Yes, with no practical depth limit. Every nested object produces its own named struct that references correctly from its parent field, so a sample with several levels of nesting still produces readable, separately named types instead of one large struct with inline anonymous fields.
No. JSON.parse turns your sample into real JavaScript values, and the field and struct inference walks that value tree using plain JavaScript already running in the tab. A real API response containing production identifiers is read only by your own browser while the types.go file is assembled.