Convert JSON to Go Structs!

Input JSON to convert it into Go (Golang) structs with json tags!
JSON
Result

Convert JSON to Go Structs!

Convert JSON into Go struct definitions with encoding/json tags. Field names follow Go's exported-name rule and golint's initialism list (ID, URL, API, ...), and columns are aligned as if the code had been through gofmt.

It is useful when you need a type to json.Unmarshal a webhook payload or API response into, a request-body struct for net/http, Gin or Echo handlers, or a config struct for a CLI.

How to use

  1. Paste the JSON. With an array of several rows, fields missing from some rows are detected and tagged omitempty.
  2. Enter the top-level struct name in Struct name (default Root); it stays in the URL as ?name=.
  3. Paste the result into a .go file and add a package clause. If time fields were found, import "time" is included.
  4. Use it as var ev WebhookEvent; err := json.Unmarshal(body, &ev).

Example

Input (JSON)
{
  "event_id": "evt_1Pq",
  "type": "invoice.paid",
  "created_at": "2024-05-01T03:15:00Z",
  "amount_cents": 4999,
  "customer": { "id": "cus_9", "email": "kim@example.com" },
  "retry_count": null
}
Output (Go)
import "time"

type WebhookEvent struct {
	EventID     string    `json:"event_id"`
	Type        string    `json:"type"`
	CreatedAt   time.Time `json:"created_at"`
	AmountCents int       `json:"amount_cents"`
	Customer    Customer  `json:"customer"`
	RetryCount  any       `json:"retry_count,omitempty"`
}

type Customer struct {
	ID    string `json:"id"`
	Email string `json:"email"`
}

event_id becomes EventID under golint's initialism rule, and created_at, an RFC 3339 string, is inferred as time.Time, which adds import "time". retry_count is null, so its type is unknown: it becomes any with omitempty.

Supported features

  • A json:"originalKey" tag on every field, plus ,omitempty for fields missing from some rows or containing null.
  • Optional value types (int, string, bool, structs, time.Time) become pointers (*int) so "absent" differs from the zero value. Slices and any are already nilable and stay non-pointer.
  • Numbers: int for 32-bit integers, int64 beyond that, float64 when decimals appear.
  • RFC 3339 strings such as 2024-05-01T03:15:00Z are inferred as time.Time.
  • Common initialisms stay upper-case (user_id → UserID, home_url → HomeURL); keys starting with a digit get a Field prefix.
  • Keys that can't appear in a Go json tag, such as "", are skipped and listed in a comment above the struct.
  • A top-level array of objects becomes a struct for the element plus a comment to unmarshal into []Root.

Use cases

Webhook receivers

Build a struct from a sample event sent by a payment or Git hosting service and decode it with json.NewDecoder(r.Body).Decode(&ev), instead of repeated type assertions on map[string]any.

Request binding in Gin or Echo

Generate the struct for c.ShouldBindJSON(&req) from a client request sample, then add binding:"required" tags where needed.

CLIs that read JSON config

Turn config.json into a struct once, and a single json.Unmarshal after os.ReadFile gives you the whole config as typed values.

Notes and limitations

  • time.Time is only inferred for RFC 3339 strings; values like 2024-05-01 03:15:00 or Unix timestamps stay string or int.
  • json.Unmarshal ignores unknown keys and matches keys case-insensitively by default. Use Decoder.DisallowUnknownFields() if you want strict decoding.
  • int is 64-bit on 64-bit platforms, but int64 is written only when a sample value exceeds 32 bits. For IDs that can grow, switching to int64 yourself is safer.
  • Nested structs are all defined as separate top-level types; no anonymous inline structs are used.
  • Mixed-type values and values that are only ever null become any; replace them once the real type is known.

FAQ

Why are optional fields pointers like *int?

A plain int field is 0 when the key is missing, indistinguishable from a real 0. A pointer is nil when the key is absent or null, and together with omitempty the key is left out when you marshal again.

Can I make the field names lower-case?

encoding/json only reads and writes exported (capitalized) fields; lower-case fields are ignored even with a tag. In Go, names stay capitalized and the JSON key goes in the tag.

A null field like retry_count came out as any.

With only null in the sample there is nothing to infer a type from. Add another response with a real value to an array and convert both, or change it to something like *int by hand.

Can I turn a Go struct back into sample JSON?

Yes. Paste the struct into the Go struct → JSON tab at the top; it reads the tags and produces example JSON.