Skip to content
DevTinyTools

JSON to Go Struct Generator

Paste a JSON response and get Go structs with json tags, int64 or float64 widths, pointers where the data was ever null and omitempty where a key was missing. Every array element is sampled, not just the first.

Runs entirely in your browser — your JSON is never uploaded.

0 chars
0 lines

Free generation covers up to 1,000,000 characters per document — what Pro adds. The JSON you paste never leaves the tab; inference runs in your browser.

How to generate Go structs from JSON

  1. Paste the JSON, or open a .json file Paste the real response rather than a trimmed sample. An array of records is better than one record, because optionality and numeric widths are only visible when records disagree.
  2. Name the root struct Root is the default; use the type you will actually reference — Order, WebhookEvent, APIResponse. Nested types are named from it, so a short root keeps the child names short too.
  3. Decide about ,omitempty and the package clause The checkbox controls whether keys that were missing from some samples get ,omitempty. The package field is optional: leave it empty to paste the structs into an existing file, or type models to get a ready-to-save package.
  4. Press Generate, then copy or download The output is already column-aligned the way gofmt aligns it, so pasting it into your editor does not produce a diff. Download writes a .go file named after the root struct.

What the generator decides, and why

TypeScript has one number type and no null-safety distinction, so a JSON-to-TypeScript converter has little to decide. Go forces four decisions on every field, and getting them wrong produces code that compiles and then fails on real data:

  • Numeric width. Whole numbers become int64, and a field with even one fractional sample widens to float64. Widening on the first decimal is the safe direction: an int64 field that receives 19.5 fails to unmarshal, while a float64 field that receives 4 unmarshals to 4.0.
  • Missing key → ,omitempty. A key that at least one sampled element omitted is treated as optional. This is the check every single-sample converter gets wrong, because the first element cannot show you what it does not have.
  • null → pointer. A value that was ever null becomes *string, so nil can represent it. Slices, maps and any are already nil-able and are not double-wrapped into *[]string.
  • Disagreeing kinds → any. Go cannot express a union, so the honest fallback is any — never a guess that discards data at unmarshal time.
  • Nesting → named structs. Each nested object is hoisted into its own declaration named after its path (OrderCustomer, OrderItemsItem) and referenced by name, because anonymous structs become unreadable after two levels and cannot be reused.

Optional is not nullable — the tag decides which

This is the part that trips up most Go code generated from JSON, and it is worth being precise about, because the two failures look identical until production.

  • Name string `json:"name,omitempty"` — the key may be absent. When you marshal, an empty string is dropped too, which is the trap: if the API distinguishes "" from a missing key, omitempty will erase the difference on the way out.
  • Coupon *string `json:"coupon"` — the value may be null. nil and "" are distinguishable, so both survive a round-trip. Use this when the API sends explicit nulls and you need to tell “no value” from “empty value”.
  • Code *string `json:"code,omitempty"` — both. The generator emits this when a key is absent from some samples and null in others, which is the only case where the two markers combine.

If you need “absent” and “zero” to stay distinct in both directions, the reliable Go answer is a pointer or a wrapper type with an IsSet flag. The generator does not invent wrappers, because it cannot know which fields your business logic needs to tell apart — it picks the simplest type that round-trips the data it was shown. The full guide covers the whole picture, including the float64 path and what omitempty really skips.

Where generated structs stop being enough

  • Dates and durations. JSON has no date type. An ISO-8601 string is a string; converting it to time.Time by hand is a one-line change, and guessing is what breaks unmarshalling when the format shifts.
  • Enums. A status field that is only ever "paid" in your sample becomes string. Define a named string type and its constants once you know the allowed values.
  • Large integers. encoding/json decodes numbers into float64 unless the target is an integer type, so an ID beyond 2^53 has already lost precision in JavaScript — but in Go an int64 field is exact. Keep monetary values out of float64 regardless: use a string or a decimal type.
  • Discriminated unions. Payloads shaped as {"type": "card", "last4": "4242"} versus different fields per variant become any on the differing members. Model those with a custom UnmarshalJSON once you know the variants.
  • Validation. Nothing here emits validate:"required" tags or binding tags. Those encode business rules the sample cannot tell you.

DevTinyTools Pro

Generating Go structs is free and stays free. Pro is for the workflows that outgrow a single paste:

  • Up to 10,000,000 characters per document — a whole API dump in one paste instead of a hand-picked fragment of it.
  • Every generator this site ships — the same licence unlocks the Zod output on the TypeScript generator and the higher limits everywhere else, including tools added after you buy.
  • Lifetime licence — one payment, no subscription, and the tool keeps working after the next round of updates.

Pro is $29 one-time — a lifetime licence, sold through Lemon Squeezy as merchant of record, so VAT and sales tax are handled for you. Pro is a licence key — there is still no account. Buy DevTinyTools Pro, then activate it below and the higher limits unlock in this browser.

Frequently asked questions

Why int64 instead of int?
Because int is 32 bits wide on 32-bit platforms and 64 bits on 64-bit ones, so the same struct that compiles and unmarshals on your laptop can silently fail on a GOARCH=386 build. API payloads routinely carry values beyond 2^31 — timestamps in milliseconds, snowflake IDs — so int64 is the width that does not depend on the target you happen to build for.
Why are optional fields tagged omitempty instead of being pointers?
They answer different questions. omitempty says the key may be absent from the JSON; a pointer says the value may be null. A field that is sometimes absent but never null is correctly modelled by a plain string with ,omitempty. Making it a *string would compile and work, but it forces a nil check at every read for no gain — and it changes what you marshal back, because a nil pointer is omitted while an empty string is not.
And the other way round — a field that is null but always present?
That one becomes a pointer, *string, with no ,omitempty. Go has no nullable string, and the zero value cannot tell you whether the API sent "" or null. A pointer can, and encoding/json unmarshals a JSON null into a nil pointer without complaining. This is why the generator keeps the two rules separate instead of collapsing both into pointers.
Why do two different types in one field become any?
Go has no union type, so the alternatives are a guess or a lie. Picking string for a field that is sometimes a number loses data at unmarshal time — encoding/json returns an error and stops. `any` (Go 1.18+, an alias for interface{}) keeps the value unmarshalled and moves the type assertion to the call site, where you can actually decide what to do with it.
How are field names chosen?
Exported PascalCase with Go's standard initialisms applied: id becomes ID, api_url becomes APIURL, user_id becomes UserID, uuid becomes UUID. The original key is preserved in the json tag, so no round-trip information is lost. Keys that are not valid identifiers — 2fa, user-name, keys with spaces — are turned into legal field names, and the tag keeps the real one.
Why is the root array type called OrderItem instead of Order?
A root-level array needs two types: the element and the slice. With the root struct named Order you get type OrderItem struct and type Order []OrderItem, which reads correctly at every use site. Rename the root struct before generating and both names follow it.
Do I get time.Time or time.Duration for dates and durations?
No, and that is deliberate: JSON has no date type, so an ISO-8601 value is a string and a duration is a number of unknown units. Guessing would produce code that fails to unmarshal the first time the API sends a slightly different format. Use string and convert at the boundary, or change the field to time.Time by hand once you know the exact layout.
Is my JSON uploaded anywhere?
No. Parsing, inference and code generation all run in the tab. The file picker reads from disk with the FileReader API and nothing is sent over the network, which is what makes it safe to paste a production response or a customer's payload.
JSON RepairFix broken JSON from an assistant or a config file: fences, comments, single quotes, trailing commas, Python literals and truncated tails — with every change reported.JSON Formatter & ValidatorPretty-print JSON with 2 or 4 space indentation, check the syntax and copy or download the result.JSON ValidatorCheck whether a JSON document is valid and see exactly where the parser stopped.JSON MinifierStrip whitespace from JSON and see how many bytes the minified output saves.JSON Diff CheckerCompare two documents and list every added, removed and changed value with its path.JSON Escape & UnescapeTurn raw text into a JSON string value, or decode backslashes and \u escapes back into readable text.JSON to CSV ConverterTurn an array of JSON objects into a CSV file you can open in a spreadsheet.JSON to TypeScript GeneratorGenerate TypeScript interfaces from a JSON sample, with optional properties and unions inferred from every array element.CSV to JSON ConverterConvert CSV to JSON with RFC 4180 parsing, delimiter detection, dotted headers unflattened into nested objects and numbers converted only when they round-trip exactly.JSON to Java POJO GeneratorGenerate Java classes from JSON: primitives for required values, boxed types for optional and nullable ones, @JsonProperty for renamed keys and nested static classes.JSON to YAML Converter (and back)Convert JSON to YAML or YAML to JSON in both directions, with multi-document support, error line numbers and YAML 1.2 typing rules.JSON to Python Class GeneratorGenerate Pydantic v2 models or dataclasses from JSON, with snake_case fields, Optional where a key is missing or null, and keyword keys renamed safely.JSON to C# Class GeneratorGenerate C# classes from JSON with nullable annotations taken from every record, initialisers that avoid CS8618 and [JsonPropertyName] for each wire key.JSON to JSON Schema GeneratorTurn a JSON sample into a draft 2020-12 schema: required keys, nullable unions and nested $defs.