FormatterHQ

JSON to Go Struct Generator

Generate Go structs from JSON: exported fields with json struct tags, nested types, slices, pointers with omitempty for optional fields, and time.Time for dates.

Input JSON
Output GO
Ready.

How to use the JSON to Go

  1. Paste your JSON sample (an object, or an array of objects) into the Input editor, or use Sample, Upload file or Load URL, or press Sample to try one.
  2. Set the Top-level class name, for example Task; it is converted to PascalCase. Nested types are named after their property keys.
  3. Tick Types only (no helpers) for structs alone, or leave it off to add a package clause and Unmarshal/Marshal helper functions.
  4. Press Generate (it also re-runs automatically as you edit), then use Copy or Download.

JSON to Go: structs with json tags

Go's encoding/json package decodes JSON into structs, but only into exported fields, and it relies on struct tags when the Go field name differs from the JSON key. This JSON to Go generator writes those structs from a sample: exported PascalCase fields, a json:"..." tag on every field, one named struct per object shape, and Go types chosen from the values.

Example

{
  "id": 42,
  "title": "Write docs",
  "done": false,
  "priority": 2.5,
  "tags": ["docs"],
  "owner": { "name": "Ada", "email": null },
  "due": "2026-10-01T09:00:00Z",
  "subtasks": [
    { "title": "Outline", "estimate": 2 },
    { "title": "Draft" }
  ]
}
type Task struct {
	ID       int64     `json:"id"`
	Title    string    `json:"title"`
	Done     bool      `json:"done"`
	Priority float64   `json:"priority"`
	Tags     []string  `json:"tags"`
	Owner    Owner     `json:"owner"`
	Due      time.Time `json:"due"`
	Subtasks []Subtask `json:"subtasks"`
}

type Owner struct {
	Name  string      `json:"name"`
	Email interface{} `json:"email"`
}

type Subtask struct {
	Title    string `json:"title"`
	Estimate *int64 `json:"estimate,omitempty"`
}

Several Go idioms are visible. ID follows Go's initialism convention (not Id). The optional estimate is a pointer, *int64, so you can tell "missing" (nil) apart from zero, and its tag has omitempty so it is left out when encoding a nil value. time.Time decodes RFC 3339 strings automatically. The null-only email became interface{}.

Helpers in full mode

Without Types only, the output starts with package main, imports encoding/json, and adds two small helpers for the top-level type:

task, err := UnmarshalTask(data)   // func UnmarshalTask([]byte) (Task, error)
bytes, err := task.Marshal()        // func (r *Task) Marshal() ([]byte, error)

They are thin wrappers around json.Unmarshal and json.Marshal. Change the package name to match your code.

Type mapping

JSONGo
integerint64 (*int64 if optional)
decimalfloat64
RFC 3339 date-timetime.Time
array[]T
objectnamed struct
null / unknowninterface{}
mixed-type valuesa struct with one pointer field per possible type

Decoding tips

For large responses, decode directly from the HTTP body with json.NewDecoder(resp.Body).Decode(&task) instead of reading everything into memory first. When numbers must keep full precision, for example IDs larger than 2^53 inside interface{} values, call decoder.UseNumber() so they arrive as json.Number rather than float64. And remember that encoding/json silently leaves fields at their zero value when a key is missing, which is another reason optional values are pointers in the generated structs.

Things to adjust

  • Date-only strings such as "2026-01-15" are not valid RFC 3339, so they are kept as string; time.Time would fail to decode them.
  • Large integers: if IDs exceed 2^53 and pass through JavaScript clients, APIs often send them as strings; keep them as strings in Go too, or use json:",string".
  • interface{} can be written any in Go 1.18+.
  • Unknown keys are ignored by encoding/json; use Decoder.DisallowUnknownFields() to reject them.
  • Mixed types generate wrapper structs that need custom UnmarshalJSON methods, which full mode includes. Prefer consistent JSON types where you control the API.
  • Shared shapes: objects with identical structure under different keys are merged into one struct type named after one of the keys. Rename it if the name is misleading; the json tags are unaffected.

Structs from XML are covered by XML to Go. Validate or pretty print your JSON with the JSON Formatter, and compare two payloads with JSON Diff. Generation runs locally in your browser; run gofmt on the result to align it with your editor's formatting.

Frequently asked questions

Why are optional fields pointers?
A pointer can be nil, which distinguishes a missing value from the zero value. Without it, a missing estimate and an estimate of 0 would look the same.
Why is the field called ID and not Id?
Go convention writes initialisms in consistent case, so id becomes ID and url becomes URL. The json tag still maps it to the original key.
Does the output use time.Time for dates?
Yes, for RFC 3339 date-time strings. Date-only values stay string, because time.Time cannot decode them without a custom type.
What package name is used?
Full mode declares package main. Change it to your package name; types-only output has no package clause, so you can paste it into an existing file.
Can I use this for YAML too?
The structs have json tags. For YAML, convert your data to JSON first, then add yaml tags alongside the json ones.

Last updated September 26, 2026

Related tools

XML to Go Generate Go structs with json tags from a XML sample instantly, with nested types detected automatically. JSON to Haskell Generate Haskell records with Aeson instances from a JSON sample instantly, with nested types detected automatically. JSON to Java Generate Java POJO classes with getters and setters from a JSON sample instantly, with nested types detected automatically. JSON to JavaScript PropTypes Generate JavaScript PropTypes React PropTypes definitions from a JSON sample instantly, with nested types detected automatically. JSON to JSON Schema Generate JSON Schema a JSON Schema document from a JSON sample instantly, with nested types detected automatically. JSON to Kotlin Generate Kotlin data classes from a JSON sample instantly, with nested types detected automatically. JSON to Laravel Model & Migration Generate a Laravel Eloquent model ($fillable, casts, relations) and a matching migration from a sample JSON record. JSON to Mongoose Schema Generate a Mongoose schema and model from a sample JSON document, detecting Date, ObjectId, arrays and nested objects. JSON to Objective-C Generate Objective-C classes with properties from a JSON sample instantly, with nested types detected automatically. JSON to PHP Generate PHP classes with typed properties from a JSON sample instantly, with nested types detected automatically. JSON to Pike Generate Pike classes from a JSON sample instantly, with nested types detected automatically. JSON to Python Generate Python dataclasses with type hints from a JSON sample instantly, with nested types detected automatically.