Go Struct Generator

Generate Go structs with JSON tags from a JSON document.

JSON Input
Go Output
Go struct output appears here

Related Tools

Documentation

What is Go Struct Generator?

This tool infers Go struct definitions from a JSON sample — the reverse of encoding/json marshaling. Paste a JSON response body and get back PascalCase struct fields with the correct json:"..." tags to round-trip that exact shape.

How it works

The input is parsed with JSON.parse, then walked recursively by value type. Objects become a new named struct — the name derived from the field's key, PascalCased (so author becomes Author) — with each of its own keys becoming a PascalCased field carrying a json:"originalKey" tag that preserves the exact original casing for marshaling. Arrays look at their first element to infer the element type ([]string, []int, or a nested []SomeStruct); an empty array falls back to []interface since there's no element to inspect. Nested object structs are emitted as their own top-level type ... struct blocks — never inlined anonymously — with the root type printed first and nested types following.

Features

  • Custom root struct name (defaults to Root)
  • Nested objects become separate named structs, not anonymous inline ones
  • Accurate json:"key" tags matching the original JSON casing exactly
  • Integers vs. floats distinguished (int vs. float64) using Number.isInteger
  • null values become interface, since Go has no bare optional primitive
  • Load from file, copy, or download as a .go file

Example

Input: {"name":"DevFormats","version":"2.0","free":true,"price":null,"tags":["json","yaml"],"author":{"type":"web-tool","clientSide":true}}

Output (root name Root):

type Root struct {
	Name string `json:"name"`
	Version string `json:"version"`
	Free bool `json:"free"`
	Price interface{} `json:"price"`
	Tags []string `json:"tags"`
	Author Author `json:"author"`
}

type Author struct {
	Type string `json:"type"`
	ClientSide bool `json:"clientSide"`
}

Common errors / edge cases

Invalid JSON surfaces the browser's native JSON.parse error, converted to a line/column so you know exactly where the syntax broke. Because the struct is inferred from one sample, a field that is a string in your example but sometimes a number in other responses (a common API inconsistency) won't be caught — the generator has no way to know about values it never saw. The output isn't run through gofmt, so struct fields aren't column-aligned; that's cosmetic only and doesn't affect compilation.

Best practices

Run gofmt or goimports on the generated file before committing. For fields that might legitimately be absent from the JSON payload (not just null), add ,omitempty to the tag yourself, and consider a pointer type instead of interface for fields you know are optional scalars, so you can distinguish "absent" from "zero value" when decoding.

Frequently Asked Questions

What json tags does it generate?

Each field gets a `json:"originalKey"` tag matching the exact JSON key, so encoding/json round-trips correctly even though Go field names are PascalCased.

How are null values handled?

A null value produces interface{}, since Go has no built-in optional/nullable primitive types — you'd typically swap in a pointer type or a null-aware type based on your use case.

How are nested objects handled?

Each nested object gets its own named struct, derived from its key, rather than an anonymous inline struct.

Is my data uploaded anywhere?

No — generation runs entirely in your browser.