How to use the json to go struct converter
- Paste a JSON object or array into the input area.
- Choose a root type name and a package name.
- Select how nullable fields are written:
omitemptyadds the tag option,pointeruses*Type. - The Go structs appear instantly. Copy the output. This json to go struct converter updates live as you type. Review the types before using them, because one sample cannot show every value the data may take.
How the json to go struct conversion works
The converter reads the JSON with the built-in JSON.parse and infers a Go type for every value:
- Whole numbers become
int64; numbers with a fraction, or too large for int64, becomefloat64. A field that is an integer in one array element and a fraction in another becomesfloat64. - Booleans become
booland strings becomestring. - Arrays become slices. All elements of an array are merged into one element type, so an array of objects produces one struct that covers every key seen. Arrays mixing different kinds, such as
[1, "a"], become[]any. - Objects become named structs; the struct name comes from the key (a key
itemsgivesItem), with the parent name added in front if that name is already taken. nullon its own, and empty arrays, becomeanyor[]anybecause the real type cannot be known from the sample.
Field names are converted to Go's exported PascalCase. Words are split at underscores, hyphens, spaces and camelCase boundaries, and common initialisms (ID, URL, URI, HTTP, HTTPS, API, JSON, XML, HTML, CSS, JS, SQL, UUID, IP, TCP, UDP, DNS, SSH, TLS, SSL, JWT, UI, CPU, RAM, RPC and a few more) are kept uppercase. For example user_id becomes UserID and apiURL becomes APIURL. Fields keep the order of the JSON, with tabs and aligned columns as gofmt would write them. The any alias needs Go 1.18 or later; use interface{} on older versions.
Generated struct example
The default input in the tool converts to:
package main
type Response struct {
UserID int64 `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
IsActive bool `json:"is_active"`
Balance float64 `json:"balance"`
Tags []string `json:"tags"`
Profile Profile `json:"profile"`
Metadata any `json:"metadata,omitempty"`
}
type Profile struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
AvatarURL string `json:"avatar_url"`
} |
With the pointer option, fields that are null or missing in some objects use pointer types instead of omitempty.
Null handling: omitempty vs pointer
Nullability is only visible when a field is null or missing somewhere in the sample, for example in one object of an array. Choose the strategy that matches your API contract.
- omitempty (default): the field keeps its concrete type and the tag gets
,omitempty. A JSONnulldecodes to the zero value, and on marshal zero values are left out. Nested structs become pointers (*Profile) becauseomitemptynever skips a struct value. - pointer: scalars and structs become pointers (
*int64,*string,*Profile). A JSONnullor a missing key decodes tonil, so you can tell zero from absent. Fields missing in some objects also get,omitempty.
A field that is only ever null in the sample is typed any in both modes, since nothing shows what it holds.
Acronym handling reference
The converter splits keys into words and keeps common initialisms uppercase in the Go field name, following the Go style guidance.
| JSON key | Go field | Note |
|---|---|---|
user_id | UserID | ID is an initialism |
apiURL | APIURL | camelCase is split into API + URL |
http_status | HTTPStatus | HTTP |
max_retries | MaxRetries | no initialism |
jwt_token | JWTToken | JWT |
ip_address | IPAddress | IP |
1st_place | Field1stPlace | a field cannot start with a digit |
Root arrays and edge cases
If the JSON root is an array of objects, the converter writes a slice type, for example type Items []ItemsItem, plus one struct for the merged element, so you can decode the array directly with json.Unmarshal. A root array of numbers or strings gives type Items []int64 or []string.
Keys that differ only in case or punctuation, such as user_id and userId, get numbered field names. Keys that cannot appear in a Go struct tag (empty keys, or keys with commas, quotes or backticks) are written with json:"-" and a comment, and the note under the output says so; decode those objects into a map[string]any. Nesting deeper than 100 levels is rejected.
Common mistakes
- Invalid JSON: trailing commas, single quotes or comments cause a parse error, and the tool shows the parser's message.
- Treating one sample as the schema: a field that is
5in your sample may be5.5elsewhere. Merging array elements helps, but check the API documentation for the real types. - Large numbers: JSON numbers are read as IEEE 754 doubles, so integers beyond
2^53-1lose precision before the type is chosen. For exact 64-bit identifiers, send them as strings. - Duplicate keys:
JSON.parsekeeps the last value, so the struct has one field.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple user object | JSON input: {"id": 42, "name": "Alice", "active": true}; Root struct name: User; Package name: models; Null handling: omitempty | package models / type User struct { / ID int64 `json:"id"` / Name string `json:"name"` / Active bool `json:"active"` / } |
| Nested with nulls and arrays | JSON input: {"order_id": "ORD-100", "items": [{"sku": "A1", "qty": 2}, {"sku": "B2", "qty": 1}], "discount": null, "metadata": {"source": "web", "campaign_id": 5}}; Root struct name: Order; Package name: orders; Null handling: pointer | package orders / type Order struct { / OrderID string `json:"order_id"` / Items []Item `json:"items"` / Discount any `json:"discount"` / Metadata Metadata `json:"metadata"` / } / type Item struct { / Sku string `json:"sku"` / Qty int64 `json:"qty"` / } / type Metadata struct { / Source string `json:"source"` / CampaignID int64 `json:"campaign_id"` / } |
| Acronyms and mixed types | JSON input: {"userID": 1, "apiURL": "https://api.example.com", "httpStatus": 200, "config": {"maxRetries": 3, "timeoutMS": 5000}, "flags": [true, false, true]}; Root struct name: Config; Package name: config; Null handling: omitempty | package config / type Config struct { / UserID int64 `json:"userID"` / APIURL string `json:"apiURL"` / HTTPStatus int64 `json:"httpStatus"` / Config ConfigConfig `json:"config"` / Flags []bool `json:"flags"` / } / type ConfigConfig struct { / MaxRetries int64 `json:"maxRetries"` / TimeoutMs int64 `json:"timeoutMS"` / } |
| Root array (wrapped) | JSON input: [{"name": "A"}, {"name": "B"}]; Root struct name: Items; Package name: main; Null handling: omitempty | package main / type Items []ItemsItem / type ItemsItem struct { / Name string `json:"name"` / } |
FAQ
Does this tool send my JSON to a server?
No. The conversion runs in your browser using the built-in JSON parser. Nothing is uploaded.
How large a JSON document can I convert?
Up to 2 MB. Larger input is rejected with a message; for very big files use a local command-line generator.
Why does my numeric ID become int64 instead of string?
JSON numbers are numbers, so a whole number becomes int64. If your IDs can exceed 2^53-1, send them as strings in JSON and change the generated field to string.
How are nested struct names generated?
From the key in PascalCase, so profile becomes Profile and an array under items becomes Item. If that name is already used, the parent name is put in front (OrderAddress), and a number is added if there is still a clash.
Can I customize the json tag key (e.g., keep snake_case)?
The tool uses the original JSON key as the tag value, so snake_case keys stay snake_case in the tag. The Go field name is always PascalCase.
What if my JSON has a key like 'type' or 'class'?
Field names are exported PascalCase (Type, Class), so they never collide with Go keywords, which are all lower-case. The lower-case key is kept in the json tag.
Does this json to go struct converter support JSON Schema or OpenAPI?
No. This tool only converts concrete JSON instances. For schema-driven generation, use a dedicated code generator.