What is a JSON to Go Struct Converter?
A JSON to Go struct converter turns a raw JSON sample into a ready-to-use Go struct definition, complete with json:"..." field tags. Instead of hand-writing a struct for every API response, config file, or database row, you paste the JSON and get accurate, compile-ready Go in a fraction of a second — entirely in your browser.
Go’s encoding/json package maps JSON into structs by matching struct tags to keys, but you still have to write the struct. For anything beyond a trivial payload, transcribing nested objects, slices, and the right numeric types by hand is tedious and easy to get wrong. This tool automates the boring part: it walks the JSON structure, infers a Go type for every value, exports and renames fields to satisfy Go’s visibility rules, attaches the correct tags, and emits clean code you can drop straight into your package.
Everything happens client-side. Your JSON never leaves the page, which matters when you’re pasting an internal API response that contains real user data or private field names. This is the Go counterpart to our JSON to TypeScript converter — same idea, idiomatic Go output.
How to Convert JSON to Go
- Paste your JSON into the input area above. Both a single object and an array of objects are supported as the top-level value.
- Click Convert (or press
Ctrl+Enter) to generate the Go struct. - Adjust the settings if needed: rename the root struct, switch between inline and named nested structs, add
omitempty, or turn on pointers for nullable and optional fields. - Copy the result with the Copy button or
Ctrl+Shift+Cand paste it into your.gofile. - Clear the workspace with
Ctrl+Lwhen you’re done.
For the most accurate results, paste an array of several records rather than a single object. With multiple samples the converter can tell which keys are always present and which are optional, and it merges every field it sees into one struct so nothing is missed.
int vs int64 vs float64: How Numbers Are Inferred
JSON has exactly one number type, but Go has many, so the converter has to make a sensible choice:
- Whole numbers become
int— the idiomatic default for counts, IDs, and sizes. - Large whole numbers beyond the signed 32-bit range (roughly ±2.1 billion, e.g. a Unix-nanosecond timestamp or a Twitter/X snowflake ID) become
int64so they never overflow on a 32-bit platform. - Numbers with a decimal point become
float64, Go’s default floating-point type. - Mixed samples — a field that is
5in one record and5.5in another — widen tofloat64so both fit.
This matters because encoding/json decodes every JSON number into float64 when you unmarshal into an interface{}. Generating a concrete struct with real integer types is the whole reason to define one: you get int64 IDs instead of lossy float64 values, and the compiler catches type mistakes for you.
Inline vs Named Nested Structs
When your JSON contains nested objects, there are two idiomatic ways to model them in Go, and this tool supports both:
- Inline (default) embeds each nested object as an anonymous
struct { ... }directly on the field. Everything stays in a single declaration, which is compact and matches the widely used json-to-go convention most Go developers recognize. - Named pulls every nested object into its own top-level type —
type Address struct { ... }— and references it by name. Choose this when a nested shape is reused across fields, when you want to attach methods to it, or when deeply nested JSON would otherwise produce an unreadable wall of indentation.
Both compile to the same wire format. Pick inline for a quick paste-and-go struct, named when the nested types deserve their own identity in your codebase.
Optional Fields, null, omitempty, and Pointers
Two of the most common sources of runtime bugs are missing fields and null fields, and Go models them differently from a language with built-in optionals:
- A missing key (present in some records of an array, absent in others) is detected as optional. Enable omitempty and it gets
json:"avatar,omitempty", so the field is omitted from the JSON output when it holds a zero value. - A null value can’t be represented by a plain value type —
0,"", andfalseare indistinguishable from a real zero. Enable Pointers for nullable/optional and those fields become pointer types like*stringor*int, wherenilmeans “absent or null” and a non-nil pointer means “present.”
Both options are off by default so the baseline output matches the familiar json-to-go format. Turn them on when you need to round-trip data faithfully — for example, distinguishing “the user did not send an age” from “the user’s age is 0.”
Common Errors and Edge Cases
- Invalid JSON. The most frequent issue is a trailing comma, single quotes, or an unquoted key — none of which are legal JSON. The tool reports the exact parse error. Run your text through a JSON formatter or JSON validator first if you’re unsure.
- A single sample object. With only one record, every key looks required. If your production data has optional fields, paste a representative array so optionality is detected correctly.
- Empty arrays.
[]gives the converter nothing to infer from, so it produces[]interface{}. Replace it with a populated sample if you need a real element type. - Heterogeneous arrays. Go has no union types, so an array like
[1, "a", true]becomes[]interface{}. Keep arrays homogeneous where you can, or handle the mixed values after unmarshaling. - Numbers that are really enums or flags.
"status": 1becomesint, not a named constant. Define your own typed constants if the field is an enum. - Timestamps as strings. ISO-8601 strings arrive as
string. Go can’t know they’re times — change the field totime.Timeyourself and they’ll parse automatically viaencoding/json.
JSON to Go Use Cases
Go developers reach for a JSON-to-struct converter constantly:
- Typing API responses. Paste a sample response from a REST or GraphQL endpoint and get a struct to
json.Unmarshalinto, with real integer and boolean types instead ofmap[string]interface{}. - Config and manifest files. Generate structs for YAML/JSON config so your service fails fast on a malformed field instead of silently reading a zero value.
- Third-party webhooks. Turn a sample webhook body into a struct so you can handle Stripe, GitHub, or Slack events with autocomplete and compiler checks.
- Bootstrapping clients. When wrapping an external API, converting its JSON responses is the fastest way to scaffold your client’s data models.
JSON to Go vs map[string]interface
You can unmarshal any JSON into map[string]interface{} and skip the struct entirely — but you pay for it. Every access needs a type assertion, every number comes back as float64, typos in key names fail silently at runtime, and there’s no autocomplete. A generated struct gives you compile-time safety, correct numeric types, IDE support, and self-documenting field names — for the cost of one paste. For quick throwaway scripts a map is fine; for anything you maintain, generate the struct. Paste, convert, copy, done.