JSON to Go Struct Converter

Convert JSON to Go structs with json tags. Get exported field names with Go initialisms like ID and URL, int64 and float64 detection, time.Time for dates, and nested structs inline or as separate types.

Go structs

                        

Generate Go Structs from JSON

Decoding JSON in Go means writing a struct for every object, with an exported field name and a json tag for every key. This JSON to Go converter does that typing for you: paste a sample payload and copy idiomatic structs that work with encoding/json.

Field names follow Go conventions, so user_id becomes UserID and avatar_url becomes AvatarURL. Numbers are split into whole numbers and decimals, RFC 3339 timestamps can become time.Time, and every item of an array is examined. Everything runs locally in your browser.

JSON object converted to a Go struct{ "id": 1 }type structJSON to GoStructs

How to convert JSON to Go

  1. Paste a JSON sample into the input box. Use a real response with several array items for the most accurate result.
  2. Name the root struct, for example User or WebhookEvent.
  3. Pick the options: when to add omitempty, int64 or int, any or interface{}, separate or inline nested structs, time.Time and pointers.
  4. Copy the Go code into your package and decode with json.Unmarshal(data, &v).

How the structs are generated

  • Field names are split into words at underscores, hyphens and camelCase boundaries, then joined in PascalCase so they are exported. Common initialisms stay upper case, as in the Go style guide: ID, URL, API, HTTP, JSON, UUID, IP and others. A key that starts with a digit gets a Field prefix.
  • json tags keep the original key, so UserID int64 `json:"user_id"` decodes the right value. With omitempty enabled, the tag becomes `json:"coupon,omitempty"`.
  • Numbers: if every value of a field is a whole number it becomes int64 (or int); if any value has a decimal part it becomes float64.
  • Arrays become slices. Objects inside arrays are merged into one struct, named in the singular: orders gives []Order. Arrays with mixed types become []any.
  • Strings that are all RFC 3339 timestamps, such as 2024-05-01T10:30:00Z, become time.Time and the time import is added.
  • null and mixed values become any, because there is no single Go type for them. With the pointer option, a field that is sometimes null becomes *string, *int64 and so on.

JSON to Go type mapping

JSON valueGo typeNotes
Stringstringtime.Time for RFC 3339 dates (optional)
Whole numberint64 or intValues larger than int64 become float64
Decimal numberfloat64Used when any value has a fraction
true / falsebool
ObjectNamed struct or inline struct { … }Empty object: map[string]any
Array[]TEmpty or mixed: []any
nullany, or *T with pointersPointers tell null apart from the zero value

omitempty, pointers and zero values

omitempty only affects encoding: when you marshal a struct, fields holding their zero value (0, "", false, nil) are left out of the JSON. That is handy for optional fields, but it also drops a real 0 or false. The default here adds omitempty only to fields that were missing or null in your sample.

If you must know whether a value was absent, null or zero, enable pointers: a *int64 is nil when the key is missing or null. Note that omitempty never omits a time.Time or other struct value; use a pointer such as *time.Time for that.

Tips for working with the generated structs

  • Check big numbers. The browser parses JSON numbers as 64-bit floats, so integers above 253 (9,007,199,254,740,992) lose precision while converting. IDs that large are often sent as strings anyway; type them as string or json.Number.
  • Watch for 1.0. A value written as 1.0 is read as a whole number. If a field can hold decimals, change it to float64.
  • Separate structs are easier to reuse and test; inline structs keep a one-off payload in one place.
  • Reject unknown fields when you need strict input: use json.NewDecoder(r) with DisallowUnknownFields().
  • Sharing the API with a frontend? Generate matching interfaces with the JSON to TypeScript converter. Format the queries behind the API with the SQL formatter.

Frequently Asked Questions

Common questions about converting JSON to Go structs.

Paste the JSON above and copy the generated code. Each object becomes a struct with exported fields and json tags, ready for json.Unmarshal.

encoding/json can only set exported fields, and a Go identifier is exported when it starts with an upper-case letter. The json tag keeps the original key name.

Go style keeps initialisms in a consistent case, so ID, URL, HTTP and API stay upper case. The converter applies the same list of initialisms that Go linters use.

Use it on fields that are optional when you encode JSON. It removes zero values from the output, so avoid it where 0, false or an empty string are meaningful.

int is 64 bits on all common 64-bit platforms, but int64 is the same size everywhere. int64 is the safer default for IDs and counters from an API.

None: any is an alias for interface{} added in Go 1.18. Choose interface{} only if you must support older Go versions.

Strings in RFC 3339 format, such as 2024-05-01T10:30:00Z, become time.Time when the option is on. time.Time decodes that format by default.

No. The structs are generated in your browser with JavaScript, so your data stays on your computer.