Developer Tools

JSON to Go Struct

Paste JSON and get Go struct definitions ready for encoding/json. Field names are exported in Go style, with ID and URL capitalised as initialisms, every field carries a json tag with the original key, optional fields can get omitempty and pointer types, and ISO dates can become time.Time. Columns are aligned the way gofmt aligns them.

  • Runs in your browser
  • No sign-up
  • Free to use

How to use JSON to Go Struct

  1. Paste a JSON sample.
  2. Set the root struct name and package.
  3. Choose omitempty and pointer options.
  4. Copy the code or download models.go.

JSON to Go Struct features

Idiomatic names

Exported PascalCase with Go initialisms such as ID, URL and API.

json tags

Every field mapped to its exact JSON key.

Optional fields

omitempty and *T pointers where values are missing or null.

Number types

int64 for integers and float64 for decimals.

Dates

time.Time for RFC 3339 strings, with the import added.

gofmt alignment

Names, types and tags line up in columns.

When to use JSON to Go Struct

  • Unmarshalling a third-party API response in a Go service.
  • Defining request and response types for an HTTP handler.
  • Reading JSON configuration into typed structs.
  • Prototyping a Go client for a REST API.

JSON to Go Struct FAQ

Why are pointers used for some fields?

A pointer can be nil, which distinguishes “missing or null” from the zero value. Without it, a missing number unmarshals as 0 and a missing bool as false.

What does omitempty do?

When marshalling, fields with zero values (or nil pointers) are left out of the JSON. It does not affect unmarshalling.

Why int64 and not int?

int64 has the same size on every platform and holds any integer JSON is likely to contain. Change it to int if you prefer.

What type is used for mixed values?

any (interface{}), which can hold whatever encoding/json decodes.

Are nested objects separate types?

Yes. Each nested object becomes a named struct, which is easier to reuse and test than anonymous structs.

Is my JSON uploaded?

No. The structs are generated in your browser.

Go structs for JSON

Go’s encoding/json package maps JSON objects onto structs, matching keys to exported fields. Only exported fields, whose names start with an upper-case letter, are visible to the package, so JSON keys like "username" need a field called Username and a struct tag saying json:"username". Writing those tags by hand for every field is the most tedious part of consuming an API in Go.

The generator writes them for you, following Go conventions. Field names are PascalCase, and common initialisms such as ID, URL, API and HTTP are written in capitals, so "userId" becomes UserID. The tag always contains the original key, so the mapping is exact even when the conversion changes the spelling.

Go has zero values instead of null: a missing string is "", a missing number 0 and a missing bool false. That is often ambiguous. When the sample shows a field missing or null, the pointer option makes it *string or *int64, which is nil when the value was absent. omitempty does the reverse when encoding, leaving zero values out of the JSON.

Numbers become int64 or float64 depending on whether the sample contains decimals; if a field can contain both, float64 is used. Dates in RFC 3339 format, the format Go’s time package expects, can become time.Time. Values whose type varies become any, and arrays become slices of the element type.

The output is aligned in columns as gofmt would format it, so it can be pasted straight into a project. Review the result against the API documentation: a single sample cannot show every optional field, and you may want to replace some types, such as string fields with a fixed set of values, with dedicated types.

Other useful tools