deftools.io Developer Tools

JSON → Go Struct

Generate Go structs from JSON. Paste JSON, get type definitions with json tags, omitempty, embedded structs, and pointer nullables.

About this tool

Generate Go struct definitions from any JSON. Paste a JSON object (or array) and the tool walks the structure, infers int vs float64, expands nested objects into named structs, and emits properly aligned Go source with json: tags. It is the Go equivalent of pasting JSON into a TypeScript generator.

Example input:

{"id":1,"name":"Ada","tags":["math","engine"]}

Produces (with default options):

type Root struct {
    ID   int      `json:"id,omitempty"`
    Name string   `json:"name,omitempty"`
    Tags []string `json:"tags,omitempty"`
}

Toggle Pointers for nullables to emit *string, *int etc. for fields whose JSON value is null — useful when you need to distinguish "absent" from "zero value" after unmarshalling. Edit the root type name to match your domain (e.g. User, Order).

FAQ

How are field names converted?

JSON keys are converted from snake_case, kebab-case, or camelCase into Go-style PascalCase (e.g. first_name → FirstName). The original JSON key is preserved in the json tag so marshalling round-trips correctly. Keywords like "type" or "func" are suffixed to avoid collisions.

How does the tool pick int vs float64?

A numeric value with no fractional part and an absolute value up to ~2.1 billion becomes int; everything else (decimals, large values, Infinity, NaN) becomes float64. Booleans become bool, strings become string.

What happens with empty arrays?

An empty array has no element to sample, so it is typed as []interface{}. Once you add at least one element the tool infers the concrete element type, including nested structs.

Are duplicate object shapes merged?

Yes. Objects with identical field names and types share one struct definition, so {"user": {"id":1}} and {"author": {"id":2}} both produce a single reusable struct instead of two identical ones.

What if I need GraphQL types instead of Go structs?

Use the <a href="/json-to-graphql">JSON to GraphQL Schema</a> tool — paste the same JSON and get GraphQL type definitions with nullable/non-nullable fields, suitable for a .graphql schema file.

More developer tools

Copied!