01What is JSON to Go Structs?
Writing Go structs for a JSON payload by hand is slow and easy to get wrong. You miss a field, pick int where the API sends 9.99, or forget the tag. Paste a JSON sample here and the tool writes the type declarations for you, ready for the standard encoding/json package. Each JSON object becomes a named struct. Fields are exported and carry a json tag with the original key, so "user_id" becomes UserID with json:"user_id". Names follow the initialism list in Go's Code Review Comments, so ID, URL, API, HTTP and friends stay upper case. A key that starts with a digit gets a Field prefix, since Go names can't start with one. If two keys map to the same Go name, like user_id and userId, the second gets a number instead of colliding. Nested objects get their own structs, printed with the root first. Arrays of objects are merged, so the element struct holds every key seen in any element. Keys missing from some elements get ,omitempty. If such a key holds an object, the field becomes a pointer, because omitempty never skips a struct value. A value that is sometimes null also becomes a pointer, so you can tell null from a zero value. Numbers follow a simple rule. Whole numbers map to int64, and a single value with a fraction makes the field float64. Mixed kinds, always-null values and empty arrays fall back to interface{}. A root array becomes a slice type, and the element name is a singular guess, so Users becomes []User. Here's a short worked case. Paste a root array of two posts named Posts, where one author is an object and the other is null. You get type Posts []Post, a Post struct with Author *Author, and an Author struct with a Name string field. Some keys can't live in a struct tag. The encoding/json docs only allow certain characters in a tag name, so keys with quotes, backslashes, commas or backticks get json:"-". A comment above the struct tells you to decode them with a map or a custom UnmarshalJSON. Limits worth knowing: the output has no package clause or imports. Types are inferred from one sample, so rare fields won't appear if the sample lacks them. Fields are aligned like gofmt does it, but run gofmt anyway. Parsing happens in your browser.
Go developers building API clients, backend engineers writing handlers, and anyone converting JSON configs or payloads into typed Go code. The tool runs entirely in your browser, so your data never leaves your device.
02How to use JSON to Go Structs
- Paste a JSON object or array into JSON Input, or load one of the examples
- Set the struct name; it becomes the top-level Go type
- Read the generated structs; any JSON syntax error appears below the editors
- Click Copy, paste into a .go file and run gofmt
03Worked examples
- Input:
{"id":1,"name":"Ada","email":null} (root name User) - Result:
type User struct { ID int64 `json:"id"` Name string `json:"name"` Email interface{} `json:"email"` } - "id" becomes ID (Go initialism), and an always-null field becomes interface{}.
- Input:
{"users":[{"id":1,"tags":["a"]},{"id":2,"nickname":"bo"}]} - Result:
type Root struct { Users []User `json:"users"` } type User struct { ID int64 `json:"id"` Tags []string `json:"tags,omitempty"` Nickname string `json:"nickname,omitempty"` } - Fields missing from some array items get omitempty.
- Input:
[{"price":9.99},{"price":10}] (root name Item) - Result:
type Item []ItemItem type ItemItem struct { Price float64 `json:"price"` } - One decimal value is enough to make the field float64 even though 10 is whole.
- Input:
{"user-id":"x","2fa":true,"apiURL":"https://x"} - Result:
type Root struct { UserID string `json:"user-id"` Field2fa bool `json:"2fa"` APIURL string `json:"apiURL"` } - A key starting with a digit gets a Field prefix, since Go names can't start with a digit; the json tag keeps the original key.
- Input:
{"items":[]} - Result:
type Root struct { Items []interface{} `json:"items"` } - Input:
[{"title":"a","author":{"name":"x"}},{"title":"b","author":null}] (struct name Posts) - Result:
type Posts []Post type Post struct { Title string `json:"title"` Author *Author `json:"author"` } type Author struct { Name string `json:"name"` } - A sometimes-null object becomes a pointer, and Posts is singularised to Post.
04Common use cases
- Writing response structs for a Go HTTP client from a sample API response
- Typing a JSON config file that a Go service loads at startup
- Turning a webhook payload from Stripe or GitHub into structs before writing the handler
- Creating request and response types for a Gin, Echo or net/http handler from documented examples
- Spotting which fields are optional or nullable by pasting several real records at once
- Checking that a JSON fixture is valid before committing it, since parse errors show up right away
05Tips
- Paste several array elements, not just one, so optional keys and nullable values show up in the sample and get omitempty or pointer types.
- If an integer can exceed 2^53 in your data, JSON parsing in the browser already loses precision; use json.Number or a string in Go for those fields.
- interface{} fields mean the sample didn't settle on one type; replace them with a concrete type or json.RawMessage once you know the real shape.
- The output has no package clause or imports, so add package main (or your package name) at the top of the file.
06Related concepts
encoding/json, Struct tags, gofmt, omitempty, Pointer types.
Frequently Asked Questions
What is JSON to Go Structs?
JSON to Go Structs is a free, browser-based developer tool on AIDToolStack. Convert JSON to Go structs with proper json tags and idiomatic naming. It requires no installation or sign-up and works entirely in your browser.
Is JSON to Go Structs free to use?
Yes. No sign-up, no fees and no usage cap.
Does JSON to Go Structs store my data?
No. It runs in your browser; nothing is uploaded.