encoding/json · PascalCase · snake_case tags

Generate Go structs from JSON

Idiomatic Go structs with proper PascalCase field names, inferred types, and json struct tags — ready to paste into your project.

JSON → Go Struct Workspace
Input size
181 B
8 lines
Output size
291 B
2 structs
Root
Root
all required
Status
Valid
encoding/json
Root
input · json sample
output · go
type Address struct {
	City string `json:"city"`
	Country string `json:"country"`
}

type Root struct {
	UserId int64 `json:"user_id"`
	Name string `json:"name"`
	Email string `json:"email"`
	IsActive bool `json:"is_active"`
	Roles []string `json:"roles"`
	Address Address `json:"address"`
}
Step-by-step Tutorial

How to generate Go structs from JSON

Stop typing structs by hand. Paste a real JSON sample and get fully-tagged type Foo struct declarations with idiomatic encoding/json tags.

  1. 01

    Paste a JSON sample

    Use a representative example — API response, config, log entry.

    Inputs parse through the native JSON.parse reviver chain — strict RFC 8259. Any syntax error returns line and column for the exact offending byte.

    sampleRFC 8259JSON.parsestrictlive
    input · raw.json · RFC 8259
    {"user_id":1}
    loaded · parsed RFC 8259RFC 8259 · ready
  2. 02

    Name the root struct

    Defaults to Root. Pick PascalCase domain names.

    Indent options run through JSON.stringify(value, null, n); the second argument lets you sort, transform, or omit keys via a replacer if you ever wire one in.

    PascalCaseindentJSON.stringifyliveordered
    naming · optionsUser · live
    type User struct {
      UserID int64 `json:"user_id"`
    }
    options applied · livelive
  3. 03

    Toggle omitempty

    Enable to add ,omitempty to every JSON tag — useful when serializing optional fields.

    Large documents stream through a useMemo with stable hashing so options re-apply instantly — no full re-parse if only formatting changes.

    omitemptymemoisedinstantdiff-friendlydeterministic
    tags · renderedomitempty · ready
    Field string `json:"field,omitempty"`
    rendered · livediff-friendly
  4. 04

    Copy or download .go

    Drop into a .go file. Marshaling/unmarshaling round-trips cleanly with the original JSON.

    Download is a Blob with application/json; copy uses the Async Clipboard API. Both round-trip identically to what the page rendered.

    copydownloadclipboardblob.json
    export · copiedready · done
    ✓ Copied
    ✓ Saved: types.go
    shipped · ready to shipsuccess
Pro tips

Production-grade JSON, the right way

Stick to RFC 8259

Strict JSON has no NaN, Infinity, comments, or trailing commas. If you need those, look at JSON5 or JSON Repair.

Preserve key order for diffs

JavaScript preserves insertion order, but two parsers shouldn’t be assumed equal. Use Sort JSON Keys before diffing or hashing.

Big integers lose precision

JS numbers lose precision past 2^53−1. Transport large IDs as strings or use BigInt.

Escape inside HTML

When inlining JSON inside <script>, escape </ to <\/ so HTML parsing doesn’t close your tag early.

Validate at boundaries

Trust the wire as little as possible. Run incoming JSON through a schema (Zod / Pydantic) before it touches business logic.

Minify in production

Minify removes whitespace; gzip removes redundancy. Together they typically shrink JSON payloads 70–85% over the wire.

Use Cases

When to generate Go structs

Anywhere you'd otherwise type structs by hand to consume JSON.

HTTP client codegen

Generate response types for your API client wrapper. Output round-trips losslessly through JSON.parse + JSON.stringify, so the file you save is byte-identical to what you see. Streaming parsers, jq pipelines, and Postgres jsonb columns all accept the result without complaint.

fewer interface{}

gRPC gateway response

Quickly model response payloads for handlers. Pairs perfectly with sort-keys + diff for review-friendly fixtures in CI. Whitespace-only churn disappears and reviewers see real changes, which keeps PR comments focused on logic rather than indentation.

cleaner PR diffs

Webhook handlers

Type the body of a Stripe / Slack / GitHub webhook for safer code. Handles emoji and non-ASCII via UTF-8 throughout — your data survives copy-paste between editors, terminals, and IDEs. Combining marks and ZWJ sequences come out the other side intact.

RFC 8259 valid

Config files

Bind a config.json to a typed Config struct. Re-parses on every keystroke so syntax errors are caught at the exact character. Strict RFC 8259 — no trailing commas, no comments, no unquoted keys — keeps downstream parsers happy.

round-trip safe

Test fixtures

Type test fixture JSON for compile-time safety in tests. Streams large documents without a server round-trip — privacy stays in the tab. Heavy parsing runs through useMemo so even 5 MB payloads keep the editor responsive.

schema-friendly

Database row scans

Generate structs that match `jsonb` columns or document-store records. Output is byte-stable: same input plus same options always produces identical bytes, ideal for content-addressed caches and snapshot tests. Downloads ship with the application/json MIME so editors apply syntax highlighting.

production-ready
FAQ

Frequently asked questions

JSON keys → PascalCase Go fields. The original key is preserved in the json struct tag (e.g. user_id → UserID with `json:"user_id"`).
Null fields are typed as interface{}. Replace with a concrete *T pointer manually if the field can be both present and null.
Yes. Each nested object becomes a named struct type referenced from the parent.
Arrays use []T where T is inferred from the first element. For mixed-type arrays, edit to []interface{}.
No. All codegen runs locally.
Suggest a tool

Something missing?

Tell us what you need. We ship the top requests every two weeks.

Sent straight to the maintainers · no account needed
47 in queue·8 shipped this month
Request queued. Thanks!