JSON to Go Struct Converter
Paste a JSON object (or JSON array) to automatically generate the corresponding Go struct definitions with json tags.
Turning JSON into Go structs
Receiving an API response in Go means writing a struct that matches the JSON, together with its json tags. With a handful of fields that is no trouble, but **once the nesting deepens, walking the hierarchy while inventing type names becomes the work itself.** This tool generates the whole set of definitions, nested structures included, from nothing more than the JSON you paste in.
**Bear in mind, though, that what comes out is a type inferred from one sample.** Keys that never appeared in that sample will naturally be absent, and any field whose value was `null` becomes `interface{}`, since Go has no nullable primitive to map it onto. When every element of an array is an object, the keys are merged into a single struct and **those present in only some elements are marked `omitempty`.** Turning the output into a production struct assumes you will check these inferences against the actual API specification and correct them.
How to convert
- Paste the JSON An object or an array will do equally well; an API response can go in exactly as it arrived.
- Name the root type It starts as `Root`. **Names for the nested types are derived automatically by converting each property name to Pascal case.**
- Review the generated structs The definitions appear in hierarchical order with their json tags in place.
- Copy it out and tidy it up Run it through `gofmt`, then revisit anything that became `interface{}` or that ought to be a pointer, following the real specification.
Tips for getting more out of it
- When all elements of an array are objects, their keys are merged into a single struct. Keys missing from some elements automatically get an `omitempty` json tag.
- The root type name defaults to "Root", but you can rename it to anything you like. Struct names for nested objects are generated automatically by converting their property names to PascalCase.
- JSON null is emitted as `interface{}` since Go has no native nullable primitive type. Consider switching to a pointer type if you need stricter handling.
- The output is a lightly column-aligned draft. Running it through `gofmt` after pasting will align it to your project's standard style.
- Paste a sample JSON response from your API as-is to quickly get a starting point for the response struct used in your Go code.
Where it comes in useful
Starting an API client
Pasting the sample response from the documentation gives you an immediate first draft of the receiving struct.
Loading a configuration file
When settings are kept in JSON, the structure can be transcribed directly into a type.
Making sense of a third-party specification
**Deeply nested JSON becomes far easier to grasp once it is laid out as a set of structs.**
Preparing test fixtures
Deriving the types from a real response first reduces the chance of a mismatch when you come to write mocks.
Terms in Go type mapping
- Struct
- The Go type that groups several fields together. It corresponds to an object in JSON.
- Json tag
- The annotation written after a field in the form `json:"user_id"`, which **tells the encoder how the Go field name lines up with the JSON key.**
- Omitempty
- An option in the json tag that **leaves the field out of the output when its value is the zero value.** It does not mean the field is nullable, a distinction worth keeping in mind.
- Interface{}
- A type that accepts any value. JSON nulls and fields of varying type land here, which **makes it a useful marker for the places to revisit after generation.**
- Pascal case
- The convention of capitalising each word, as in `UserId`. In Go **that leading capital also carries the meaning of being exported from the package.**
- Pointer type
- A type written as `*string`. It is what you reach for when the absence of a value has to be told apart from an empty string.
Frequently asked questions
Side Note — Why Go Pairs Structs With json Tags
Go is a statically typed language, and when it works with JSON, the standard library's `encoding/json` package relies on the mapping between struct fields and their json tags to encode and decode data. Manually writing out a struct for an external API's response shape is a repetitive chore that grows every time a field is added, and it has been a recurring task across many Go projects.
This tool parses the structure of a sample JSON payload and automatically generates the corresponding struct definitions and json tags, cutting down on that repetitive work. A well-known tool in the same space is quicktype, which supports converting to types in multiple languages — but for the simpler case of pasting a sample API response and quickly getting a Go struct out of it, a focused single-purpose tool has its own convenience.
Because Go has no language feature for making a struct field optional, the common approach for JSON fields that might be missing is to add `omitempty` to the json tag, or to use a pointer type (like `*string`) to distinguish a zero value from "no value at all". The generated struct is only a mechanical inference from structure, so treating nullability and future API changes as something to verify and adjust by hand against the actual specification is standard practice.