JSON to Go Struct
Generate Go structs from a JSON sample, with json tags, pointers for optional fields and nested types extracted.
What was generated
Generation happens entirely in your browser, so a real API response never leaves your machine.
What It Does
Paste a JSON response and get the Go structs that decode it. Two details separate this from a straight transliteration. First, a key that appears in some array elements but not others becomes a pointer, because Go has no undefined and a plain string cannot distinguish a missing key from an empty one — that distinction was in your JSON and it should survive into your types. Second, every field carries a json tag with its original name, because Go can only encode exported fields and FirstName will silently fail to match first_name without one. Nested objects are extracted into their own named structs, identical shapes are reused rather than duplicated, and array element types are named in the singular so the code reads naturally.
When to Use It
- You are consuming a third-party API in Go and need the structs for a response with forty fields.
- A webhook payload needs a type to decode into and you have a sample of the real thing.
- You want to check whether an API's response is consistent before writing code that assumes it is.
- A configuration file in JSON needs a matching Go type for loading at startup.
- You are writing tests and need a struct that matches a fixture you already have.
Worked Examples
{"user_id":1,"first_name":"Jane","is_active":true}
Shows the tag rule clearly. Every field is renamed to its exported form and tagged with the original key — without those tags the standard library would look for UserId in the JSON and find nothing.
[{"id":1,"nickname":"jo"},{"id":2}]
The pointer case. Nickname is absent from the second record, so it becomes a *string with omitempty — which is the only way Go can tell "not sent" from "sent as empty".
{"page":1,"results":[{"id":9,"author":{"name":"Jane"}}]}
Nesting. The result element becomes its own Result struct named in the singular, the author becomes an Author struct, and the outer type references both rather than inlining them.
Features
How to Use
1. Paste your JSON — a full array of records gives the best result, since that is what reveals optional fields. 2. Set a name for the root struct. 3. The Go code appears immediately below. 4. Adjust the pointer and omitempty options to match your codebase's conventions. 5. Copy the structs, then change float64 to int64 wherever you know the field is integral.
Common Mistakes
- Deleting the struct tags. Go looks for the tagged name when decoding, so removing them makes every snake_case field silently stay empty with no error at all.
- Using a plain type for an optional field. Without a pointer, a missing key and an empty value are indistinguishable, and code downstream cannot tell which it got.
- Leaving lowercase field names. Unexported fields are invisible to encoding/json entirely, so they neither encode nor decode.
- Assuming float64 is wrong and changing everything to int. It is the safe default because JSON has one number type — change it only where you know the value is always integral.
- Treating generated structs as finished. They are structurally correct but know nothing of your intent about enums, renames or custom unmarshalling.