JSON to Go Struct Converter
Paste a sample JSON object or array to generate matching Go structs with json tags. Nested objects become their own named structs.
How to use it
Paste a representative JSON sample and click Generate. Field names are converted to exported (capitalized) Go identifiers, with a json:"originalKey" tag so encoding/json still maps to the original key. Types are inferred from the single sample provided.
From JSON objects to Go structs
The converter creates exported fields because Go's JSON package cannot populate unexported fields. Each field retains the original wire name in a JSON tag, allowing a Go-style identifier to map to snake_case, kebab-case, or other JSON keys. Nested objects become additional struct declarations, and arrays become slices when a consistent element type can be inferred.
Review nullability and missing fields
A single payload cannot distinguish a required zero value from a missing or nullable field. A null sample therefore falls back to a broad type. In production models, choose pointers, nullable wrapper types, or custom unmarshalling only where the contract requires them. Add omitempty deliberately: it affects encoding and can hide zero values, but it does not make incoming data valid or enforce required fields.
Numbers need contract knowledge
JSON has one number syntax, while Go has many integer and floating-point types. An observed whole number can suggest int, but identifier size, currency precision, overflow limits, and decimals require human decisions. Large JSON integers may need int64, strings, or a decoder configured with json.Number. Do not accept a generated numeric type solely because the example happened to be small.
Mixed arrays and nested names
When array elements do not share a usable static type, the result falls back to []interface{}. Replace that with a documented union model, a discriminated structure, or custom decoding if callers rely on the variants. Generated nested names are conveniences based on property paths. Rename them to package-appropriate domain terms and check collisions before exposing the structs as a stable public API.
Compile, validate, and test the result
Run the generated code through formatting and the compiler, then unmarshal several representative fixtures: complete, minimal, null-heavy, empty-array, error, and boundary cases. Struct types alone do not enforce required fields, string formats, or numeric ranges, so keep explicit validation at the service boundary. Compare against a JSON Schema or OpenAPI contract when available. Pasted data and generated structs remain in this browser tab.
Frequently Asked Questions
What if an array has mixed element types?
Go has no union types, so a mixed-type slice falls back to []interface{}. A homogeneous array becomes []T.
What about null values?
A null field is typed interface{}, since Go's static types can't represent "string or nothing" without more context than a single sample provides.
What if two nested objects need the same struct name?
If they have an identical shape, the struct is reused. If the shapes differ, the second gets a numeric suffix (e.g. Address2) to avoid a name collision.