PureDevTools

JSON to Go Struct Generator

Paste JSON, get Go structs — nested objects, arrays, json tags, omitempty — copy or download as .go

All processing happens in your browser. No data is sent to any server.

Options

428 characters

4 structs generated · 605 characters

Structs generated: 4Output size: 605 charsOptions: exported · json tags

You’re integrating a REST API in Go and need a struct that matches the JSON response. The response has 30 fields, nested objects, arrays of mixed types, and fields that are sometimes null. Writing the struct by hand means 30 lines of FieldName type \json:“field_name”“ tags, and one typo in the JSON tag means silent data loss — the field just stays at its zero value with no error.

Why This Generator (Not json-to-go.dev)

json-to-go.dev is the original tool by Matt Holt — and it works. This tool adds omitempty toggle, pointer types for nullable fields, and inline vs separate struct declarations — options that matter for production Go code. Everything runs in your browser; no data is sent anywhere.

What Is a JSON to Go Struct Generator?

When building Go applications that consume REST APIs, parse configuration files, or process any JSON data, you need Go struct types that match the JSON structure precisely. Writing these structs by hand is tedious — especially for deeply nested JSON with many fields.

A JSON to Go struct generator reads your JSON and automatically produces correct Go struct declarations, handling:

This tool runs entirely in your browser — your JSON data never leaves your device.

How to Use This Tool

  1. Paste your JSON in the JSON Input textarea (or click Load sample to try an example)
  2. Configure options: root struct name, export fields, JSON tags, omitempty, inline structs, pointer types
  3. The Go struct output updates instantly in the Go Output panel
  4. Click Copy to copy to clipboard, or Download .go to save as a .go file

Understanding the Generated Structs

Field Naming Convention

JSON keys are converted to idiomatic Go field names using the following rules:

Struct Naming

Nested struct names are derived from the parent struct name plus the field name in PascalCase:

Type Mapping

JSON typeGo type
stringstring
integer numberint
floating-point numberfloat64
booleanbool
nullinterface{}
object {}named struct
array of objects[]StructName
empty array[]interface{}
mixed-type array[]interface{}

Options Explained

Root Name

The name given to the top-level struct. Defaults to Root. If your JSON represents a User record, set the root name to User for cleaner, idiomatic Go code.

Export Fields

When enabled (default), all field names start with an uppercase letter, making them exported (publicly accessible). Disable for unexported fields — uncommon but occasionally useful in internal packages.

JSON Tags

When enabled (default), each field gets a json:"key" struct tag. These tags are required by encoding/json for correct field mapping when the JSON key differs from the Go field name (e.g., snake_case JSON keys with PascalCase Go fields).

Example with JSON tags:

type User struct {
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	IsActive  bool   `json:"is_active"`
}

omitempty

Appends ,omitempty to every JSON tag. When marshaling to JSON with encoding/json, fields with zero values ("", 0, false, nil) are omitted from the output:

type User struct {
	Name  string `json:"name,omitempty"`
	Email string `json:"email,omitempty"`
}

Useful for API request payloads where omitting zero-value fields is desirable.

Inline Structs

By default, nested structs are declared as separate top-level types. With inline structs enabled, nested types are declared as anonymous structs within the parent:

Separate (default):

type Root struct {
	Profile RootProfile `json:"profile"`
}

type RootProfile struct {
	Bio string `json:"bio"`
}

Inline:

type Root struct {
	Profile struct {
		Bio string `json:"bio"`
	} `json:"profile"`
}

Separate declarations are generally preferred — they are reusable, testable, and easier to reference by name.

Pointer Types

When enabled, optional fields (present in some but not all array elements) are generated as pointer types (*string, *int, *bool) instead of value types. This distinguishes between a missing field (nil) and a zero-value field ("", 0, false):

type RootPostsItem struct {
	ID        int     `json:"id"`
	Title     string  `json:"title"`
	Published *bool   `json:"published"`  // optional: nil if absent
}

Handling Arrays of Objects

When a JSON array contains objects, the generator merges all objects in the array into a single struct. Properties that exist in only some of the elements are still included — the Go type system doesn’t have per-field optionality, so all fields appear in the struct:

Input JSON:

{
  "posts": [
    { "id": 1, "title": "Hello World", "published": true },
    { "id": 2, "title": "Go Tips" }
  ]
}

Generated output:

type Root struct {
	Posts []RootPostsItem `json:"posts"`
}

type RootPostsItem struct {
	ID        int    `json:"id"`
	Title     string `json:"title"`
	Published bool   `json:"published"`
}

With pointer types enabled, Published becomes *bool since it’s absent from some objects.

Handling null Values

JSON null maps to interface{}:

type Root struct {
	Name      string      `json:"name"`
	DeletedAt interface{} `json:"deleted_at"`
}

In practice, you may want to refine this to *string or a custom nullable type. The tool generates types based on the actual data provided.

Common Use Cases

Consuming REST APIs

Paste the raw JSON response from a REST API endpoint to instantly generate Go structs for unmarshaling:

resp, err := http.Get("https://api.example.com/users/1")
var user User
json.NewDecoder(resp.Body).Decode(&user)

Configuration File Parsing

If your Go application reads a JSON configuration file, generate structs from a sample config to get type-safe access to all configuration fields.

Third-Party API Integration

Most REST APIs provide sample responses in their documentation. Paste the sample and get the struct definitions needed for your integration layer.

gRPC/Protobuf Migration

When migrating from JSON-based APIs to gRPC, use the generated structs as a reference for defining your .proto message types.

After Generating: Refining Your Structs

The auto-generated structs are a solid starting point. Consider these refinements:

  1. Add time.Time for date/time fields:

    CreatedAt time.Time `json:"created_at"`
  2. Use custom types for string enums:

    type Status string
    const (
        StatusActive   Status = "active"
        StatusInactive Status = "inactive"
    )
  3. Add validation struct tags (for libraries like go-playground/validator):

    Email string `json:"email" validate:"required,email"`
  4. Deduplicate identical structs — if two generated structs are structurally identical, consolidate them into one.

  5. Add methods — attach Validate(), String(), or other methods to named types.

FAQ

Does this tool send my JSON to a server?

No. All processing happens entirely in your browser using JavaScript. Your JSON data never leaves your device.

What Go package does the download include?

Downloaded .go files include package main at the top. Change this to the appropriate package name for your project (package models, package types, etc.).

How are empty objects {} handled?

An empty JSON object generates a struct with no fields:

type Root struct{}

What about duplicate struct names?

If two different JSON paths produce the same struct name, the generator appends a numeric suffix (RootItem, RootItem2) to avoid conflicts.

Can I use the generated structs with encoding/json directly?

Yes. The generated struct tags (json:"key") are designed for use with Go’s standard encoding/json package. Both json.Marshal and json.Unmarshal will work correctly with the default options.

What if my JSON has keys that are Go reserved words?

Go reserved words like type, func, map, range — when used as JSON keys — are capitalized to Type, Func, Map, Range. These are valid Go field names and won’t conflict with the language keywords.

Does this support Go generics?

The generator produces standard struct types. Generic struct generation (e.g., type Response[T any] struct { Data T }) is not supported — this would require knowledge of multiple related JSON shapes.

Related Tools

More JSON Tools