JSON to Go Struct
Generate Go structs with JSON tags from a sample JSON object.
or drop a file here
Go structs
Generating Go structs from JSON
Paste a representative JSON object and this tool generates matching Go structs, complete with json:"..." field tags so encoding/json marshals and unmarshals correctly. Field names are exported in PascalCase, nested objects become their own named structs, and numbers map to int or float64 based on the sample value. If you pass an array, the first element defines the shape.
Prefer TypeScript? Use JSON to TypeScript. You can also format the JSON first or turn it into SQL inserts. Everything runs in your browser.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Generating structs with JSON tags from a sample response.
- Producing a starting model for an endpoint with no schema.
- Seeing how nested objects become embedded or named structs.
- Creating types for a fixture that match production shapes.
- Getting the tag names right for keys that are not valid Go identifiers.
Frequently Asked Questions
- Why do the struct tags matter so much?
- Because Go's encoding/json matches by field name case-insensitively but exports only CAPITALISED fields. Without a `json:"field_name"` tag, a snake_case API key will not round-trip correctly — the tag is what preserves the wire format.
- How should nullable fields be typed?
- As pointers (*string) or sql.NullString. A plain string cannot distinguish absent, null and empty — all three decode to "". This matters enormously for PATCH endpoints, where absent and null mean different things.
- What does omitempty actually do?
- It omits the field on MARSHAL when the value is the zero value — which means a genuine false, 0 or empty string disappears from the output. That is a real bug source when the API distinguishes false from absent.
- How are JSON numbers typed?
- As float64 by default, since JSON has one number type. A 64-bit integer ID therefore loses precision past 2^53 unless you declare it int64 explicitly or decode with UseNumber().
- How are nested objects handled?
- Either as named types or as anonymous inline structs. Named types are reusable and produce far better error messages; deeply inlined anonymous structs are technically valid and painful to work with.
Common errors and gotchas
- Accepting `interface{}` for a mixed or null field, which pushes the problem to run time.
- Letting a whole number become `int` when another response returns a float.
- Omitting `omitempty` where a zero value and an absent field mean different things.
- Exporting every field automatically, when unexported fields are silently skipped by the encoder.
- Assuming a large integer is safe as `int`, when the JSON producer may already have lost precision.
Related Converters tools
JSON to YAML
Convert JSON into clean, readable YAML instantly.
YAML to JSON
Convert YAML configuration into valid JSON.
JSON to XML
Convert JSON structures into nested XML markup.
HWB to HEX Converter
Convert an HWB color to HEX.
JSON to SQL
Turn a JSON array of objects into SQL INSERT statements.
CSV to XML
Convert CSV rows into structured XML records.
XML to CSV
Flatten repeated XML records into CSV rows.
INI to JSON
Parse INI config sections and keys into JSON.