Developer

JSON to Go Struct

Convert JSON into Go struct definitions with json tags.

Loading interactive tool…

Embed this tool on your site

About this tool

Go's strict typing system requires you to define struct types before you can unmarshal JSON, and hand-writing those structs from a complex API response is a mechanical chore — every nested object needs its own named type and every field needs a json tag. JSON to Go Struct gives you a first draft: set a Root Struct Name, paste a JSON object or array, click Convert, and receive Go struct definitions with capitalized field names and json:"fieldname" tags that map back to the original JSON keys. Whole numbers become int, decimals float64, strings string and booleans bool. Nested objects produce separate structs named after the parent type plus the key, arrays of one type become slices, and null, empty or mixed values fall back to interface{}. Output is not gofmt-aligned, and arrays containing several objects currently come out as []interface{}, so review the result before committing.

Why use it

  • Quick Go Struct Drafts: Produces capitalized, exported field names with json tags in a single step, ready for encoding/json binding.

  • Nested Types Handled Automatically: Each nested JSON object becomes its own named Go struct type, referenced from the parent — no need to manually create and link inner types.

  • Original Keys Preserved: Every field's json tag holds the exact original key, so snake_case, camelCase or hyphenated JSON keys still unmarshal correctly.

  • Ready for encoding/json: The generated structs use only Go's standard library json.Unmarshal and json.Marshal with no third-party dependencies required.

  • Saves Manual Work: A 40-key API response with several levels of nesting takes many minutes to transcribe by hand — the converter drafts it in under a second.

  • Runs in the Browser, No Go Toolchain Needed: Works on any machine without a Go installation — useful during design, planning, or when reviewing API contracts on a device without a dev environment.

How to use

  1. Set the Root Struct Name (Root by default) to the type name you want.
  2. Paste your JSON object or array into the JSON Input box — use Beautify if you want to reformat it first.
  3. Click Convert to generate the Go structs.
  4. Check the mapped types: string, int, float64, bool, nested structs, slices, and interface{} for null, empty or mixed values.
  5. Copy the generated Go code, add your package line, and run gofmt to align the fields.
  6. Use json.Unmarshal(data, &target) or json.NewDecoder(r.Body).Decode(&target) to parse live data.

When it helps

  • When integrating a REST API that returns JSON and you need Go structs to deserialize the response in your service layer.
  • When writing Go microservices that accept a JSON request body and you need struct types to decode it with json.NewDecoder.
  • When bootstrapping a Go project that consumes a third-party API with no official Go SDK and no OpenAPI spec.
  • When prototyping a new feature and you want to quickly scaffold the data types from a real JSON fixture before writing business logic.
  • When onboarding onto a Go codebase and you encounter undocumented API responses that need struct definitions written from a logged payload.
  • When converting JavaScript or Python service contracts to Go and you have example payloads to work from.

Examples

Simple user object (Root Struct Name: User)

Input
{
  "id": 1,
  "username": "gopher",
  "email": "gopher@example.com",
  "verified": true
}
Expected result
type User struct {
	Id int `json:"id"`
	Username string `json:"username"`
	Email string `json:"email"`
	Verified bool `json:"verified"`
}

Nested address object (Root Struct Name: User)

Input
{"name":"Alice","address":{"street":"123 Main St","city":"Springfield","zip":"12345"}}
Expected result
type User struct {
	Name string `json:"name"`
	Address UserAddress `json:"address"`
}

type UserAddress struct {
	Street string `json:"street"`
	City string `json:"city"`
	Zip string `json:"zip"`
}

Top-level array with one product (Root Struct Name: Products)

Input
[{"id":1,"product":"Widget","price":9.99}]
Expected result
type Products []ProductsItemItem

type ProductsItemItem struct {
	Id int `json:"id"`
	Product string `json:"product"`
	Price float64 `json:"price"`
}

Tips

  • After generating, add `omitempty` to tags for fields that are optional in the JSON payload so json.Marshal omits them when zero-valued, keeping serialized output clean.
  • If the API returns ISO 8601 date strings, change the generated string field to time.Time — encoding/json parses RFC 3339 strings into time.Time automatically.
  • Use a real API response with all fields populated as your input sample, not a minimal example — null fields come out as interface{} and need a real type.
  • Rename fields like Id and Url to Go's preferred ID and URL after generating; the json tag keeps the JSON mapping intact.
  • Rename generated inner struct types (e.g. UserAddress) to more domain-meaningful names after generation to improve readability across your package.

Frequently Asked Questions

How are JSON field names converted to Go struct field names?
The first letter of each word is capitalized and other characters are dropped, so 'userId' becomes UserId and 'first name' becomes FirstName. Underscores are removed without capitalizing the next letter, so 'first_name' becomes Firstname and 'id' becomes Id rather than ID. Every field gets a json:"original_key" tag, so encoding/json still maps it to the right JSON key.
What Go type is used for JSON numbers?
If the number has no decimal component (e.g. 42), it maps to int. If it has a decimal part (e.g. 3.14), it maps to float64. For very large integers or when precision matters, you can manually change the type to int64 or float32 after generating the struct.
How are null JSON values represented in Go?
A null value is typed as interface{}, because a single null gives no hint of the real type. If you need to tell null apart from a zero value, change the field to a pointer type such as *string or *int by hand after generating.
What happens with arrays in the JSON?
An array whose items all map to one Go type becomes a slice of it, such as []string or []int. Empty arrays and arrays that mix types (including ints mixed with decimals) become []interface{}. An array with a single object becomes a slice of a generated struct; an array with two or more objects currently becomes []interface{} with a separate struct printed for each object, so merge those into one struct by hand.
Does the generated code compile without modification?
Usually, once you add a package declaration at the top. Field alignment is a single space, so run gofmt to line up the columns. For arrays of several objects, replace []interface{} with a slice of one of the generated structs and delete the duplicates.
Does it add omitempty to struct tags?
By default the generator adds json:"fieldname" without omitempty. If you want json.Marshal to skip zero-value fields when re-serializing, manually add ,omitempty to the tag (e.g. json:"fieldname,omitempty").
Can I use the generated structs with gorilla/mux or Gin?
Yes. The structs use only the standard encoding/json package for binding. Gin's c.ShouldBindJSON() and gorilla/mux route handlers that call json.NewDecoder(r.Body).Decode(&target) work identically with generated structs.
How does the tool handle deeply nested JSON objects?
Nesting is handled recursively. Each nested object produces a new struct whose name joins its parent's type name and the PascalCase key, for example an address key inside User becomes UserAddress. Duplicate names get a number suffix.

Explore the category

Glossary

Struct Tag
A raw string literal following a Go struct field that provides metadata consumed by packages like encoding/json. The json:"fieldname" tag maps the struct field to the JSON key of the same name.
PascalCase
A naming convention where each word starts with an uppercase letter (e.g. UserProfile). In Go, PascalCase field names are exported (visible outside the package), which is required for encoding/json to marshal and unmarshal them.
Pointer Type
A Go type prefixed with * (e.g. *string) that holds the memory address of a value rather than the value itself. Pointer types can be nil, distinguishing 'not present' from a zero value.
json.Unmarshal
The standard library function that parses a JSON byte slice and populates a Go value (usually a struct pointer) according to the json struct tags on each field.
omitempty
A json struct tag option that instructs json.Marshal to skip a field in the output if the field holds its zero value (empty string, 0, false, nil pointer, empty slice).
interface{}
Go's empty interface type that can hold any value. Used in generated structs when the JSON source has mixed-type arrays or when the exact type cannot be determined from the sample.
A LITTLE HELP. A LOT DONE.
Good tools.
Great flow.

Your shortcut to the everyday. Create, convert, calculate,
and get back to what you love.

✓ Free to use✓ No sign-up✓ 500+ possibilities