UtilityKit

500+ fast, free tools. Most run in your browser only; Image & PDF tools upload files to the backend when you run them.

JSON to Go Struct

Convert JSON into Go struct definitions with json tags.

About JSON to Go Struct

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 that adds up fast — every nested object needs its own named type, every field needs a json tag, and Go's capitalization rules mean you have to think through every exported-vs-unexported decision. JSON to Go Struct automates this entirely: paste any JSON object or array and receive idiomatic Go struct definitions with correct types, proper PascalCase field names, and json:"fieldname" struct tags that map back to the original lowercase JSON keys. Nested objects produce separate named structs, arrays of objects produce slice types, and nullable fields are mapped to pointer types.

Why use JSON to Go Struct

Instant Idiomatic Go Structs

Produces PascalCase field names with json tags in a single step, following the Go community convention for JSON binding without any manual reformatting.

Nested Types Handled Automatically

Each nested JSON object becomes a named Go struct type, correctly referenced in the parent — no need to manually create and link inner types.

Pointer Types for Nullable Fields

JSON null values map to pointer types (*string, *int, *bool) so your code can distinguish between a missing field and a zero value — a common Go gotcha.

Ready for encoding/json

The generated structs integrate directly with Go's standard library json.Unmarshal and json.Marshal with no third-party dependencies required.

Saves Significant Manual Work

A 40-key API response with three levels of nesting could take 15 minutes to transcribe correctly by hand — the converter produces 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 JSON to Go Struct

  1. Paste your JSON object or array into the input panel — a real API response or fixture works best for accurate type inference.
  2. The tool inspects each key-value pair and maps the JSON type to the closest Go type: string, int, float64, bool, or a nested struct.
  3. Field names are converted to PascalCase for Go export conventions, and a json:"original_name" struct tag is added to preserve the original JSON key.
  4. Nested JSON objects generate additional named struct types, which are referenced by the parent struct field.
  5. JSON arrays of objects generate a []StructName slice type; arrays of primitives generate []string, []int, []float64, or []interface{} as appropriate.
  6. Copy the generated Go code and paste it into your .go file, then use json.Unmarshal(data, &MyStruct{}) or json.NewDecoder(r.Body).Decode(&target) to parse live data.

When to use JSON to Go Struct

  • 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

Input: { "id": 1, "username": "gopher", "email": "gopher@example.com", "verified": true }

Output: type AutoGenerated struct { ID int `json:"id"` Username string `json:"username"` Email string `json:"email"` Verified bool `json:"verified"` }

Nested address object

Input: { "name": "Alice", "address": { "street": "123 Main St", "city": "Springfield", "zip": "12345" } }

Output: type Address struct { Street string `json:"street"` City string `json:"city"` Zip string `json:"zip"` } type AutoGenerated struct { Name string `json:"name"` Address Address `json:"address"` }

Array of products

Input: [{"id":1,"product":"Widget","price":9.99,"inStock":true},{"id":2,"product":"Gadget","price":24.99,"inStock":false}]

Output: type AutoGenerated struct { ID int `json:"id"` Product string `json:"product"` Price float64 `json:"price"` InStock bool `json:"inStock"` } // Usage: var items []AutoGenerated // json.Unmarshal(data, &items)

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 and the json tag will work automatically — encoding/json handles time.Time parsing for RFC 3339 strings.
  • Use a real API response with all fields populated as your input sample, not a minimal example, to get the most accurate type mapping including nested and array fields.
  • For APIs that return inconsistent types (a field is sometimes a number, sometimes a string), change the generated type to interface{} and add a comment noting the inconsistency for future maintainers.
  • Rename generated inner struct types (e.g. Address, Metadata) 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?
JSON keys are converted to PascalCase following Go's exported field convention — for example, 'first_name' becomes FirstName and 'userId' becomes UserId. A json:"original_key" struct tag is added to each field so encoding/json maps the struct field back to the original JSON key during marshal and unmarshal.
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?
null maps to a pointer type: *string, *int, *bool, etc. This lets your code check if val == nil to detect a truly absent/null value, as opposed to an empty string or zero integer which are valid Go zero values.
What happens with arrays in the JSON?
An array of a single primitive type (all strings, all numbers) maps to []string, []int, or []float64. An array of objects produces a named struct for the element type and a slice []ElementType. Mixed-type arrays fall back to []interface{}.
Does the generated code compile without modification?
For most well-formed JSON it does. Add 'package main' or the appropriate package declaration at the top, and the structs are ready to use. You may need to rename generated inner struct types if they conflict with existing type names in your package.
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 level produces a new named struct type, named after a PascalCase version of its parent key. The depth limit is only your browser's call-stack limit — practical for all real-world API responses.

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.