JSON to Go
No content yet
Paste JSON on the left, and the right side automatically generates struct type definitions ready for direct use in Go projects. Pure browser-local processing, no registration or upload required.
No content yet
Paste JSON on the left, and the right side automatically generates struct type definitions ready for direct use in Go projects. Pure browser-local processing, no registration or upload required.
JSON to Go is the process of automatically converting JSON-formatted data into Go language struct type definitions. JSON is the most common data interchange format in REST APIs, config files, message queues and logs, while Go is a strongly-typed language. In development, it is often necessary to map JSON's dynamic structure to structs before deserializing with encoding/json. Writing structs manually, especially with nested objects, is prone to missing fields or wrong types; this tool aims to automate that repetitive labor.
Go's struct is a composite data type, declared with `type Name struct { ... }`. Each field consists of a name and type; field names must start with an uppercase letter to be accessible from other packages, known as 'exported identifiers'. The tool automatically converts JSON field names to PascalCase exported field names, e.g. `user_name` maps to `UserName`, while preserving the original key in the json tag.
The core conversion of this tool is done by the in-browser jsonFormat Web Worker, calling quicktype-core's Go renderer with `just-types` and `no-comments` options. The entire flow does not depend on backend services: input JSON is parsed locally, types inferred, Go code generated, and finally presented in the right-side editor.
Type inference follows conventional JSON-to-Go mapping: strings map to `string`, booleans to `bool`, integers to `int64`, floats to `float64`, arrays to slices `[]T`, objects to independent structs, `null` to `interface{}`. Nested objects are processed recursively, maintaining clear hierarchy.
Compared to manual writing, tool-generated code is a 'runnable first draft'. Developers typically only need to fine-tune the root type name, package name, field type precision (e.g. int64 to int) and whether to use pointer types before placing it into real Go projects. Combined with one-click copy and model.go download, it significantly reduces repetitive work during integration and modeling phases.
After copying the tool-generated struct into your project, you can directly deserialize API responses with encoding/json.
package main
import (
"encoding/json"
"fmt"
)
// 以下代码由 GeekFormat JSON 转 Go 工具生成
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Tags []string `json:"tags"`
Address Address `json:"address"`
}
type Address struct {
City string `json:"city"`
}
func main() {
data := []byte(`{
"id": 1,
"name": "Alice",
"tags": ["admin", "dev"],
"address": {"city": "Beijing"}
}`)
var u User
if err := json.Unmarshal(data, &u); err != nil {
panic(err)
}
fmt.Println(u.Name, u.Address.City)
}If certain API fields may not exist, change corresponding fields to pointer types or use with omitempty tags.
package main
import "encoding/json"
// 手动将字段改为指针,缺失时值为 nil
type User struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Email *string `json:"email,omitempty"`
}
func main() {
data := []byte(`{"id": 1, "name": "Bob"}`)
var u User
json.Unmarshal(data, &u)
if u.Email == nil {
println("Email 字段缺失")
}
}Paste JSON content into the left input box; the tool will auto-convert within 400ms and display the generated Go struct on the right. You can also drag and drop .json/.txt files, or click 'Example' to load built-in data. After conversion, copy or download the model.go file with one click.
Yes. The tool generates Go code based on quicktype-core, and by default every struct field includes a `json:"original_field_name"` tag, convenient for direct serialization and deserialization with encoding/json's Unmarshal/Marshal.
Yes. JSON arrays generate corresponding slice types based on element types, e.g. `["a","b"]` generates `[]string`, `[1,2,3]` generates `[]int64`, `[{...},{...}]` generates custom types like `[]UserItem`.
Yes. Every nested object recursively generates an independent struct type, named in PascalCase. For example, the `address` object generates an `Address` struct, referenced in the main struct via the `Address Address` field.
Null values in JSON are usually inferred as `interface{}`, which is the safe fallback type. If you know the actual type of the field, you can add an example value in JSON, regenerate, then manually adjust to the specific type.
Yes. The tool uses the root type name (e.g. User) by default; after modifying the root type name in settings, all associated subtype names update synchronously, and the downloaded filename also changes accordingly.
The tool automatically detects JSON validity. If there are common errors like trailing commas, single quotes, missing quotes, a 'Fix JSON' button appears. Clicking it will attempt automatic repair and regenerate the Go struct; when repair fails, the specific error location is indicated.
No. All JSON parsing, type inference and Go code generation complete in your browser via Web Worker; input content and generated code are never uploaded to any server, nor recorded to the cloud. Sensitive JSON containing API keys, tokens or business data can be used safely.
Yes. The generated code is standard Go structs with json tags, can be directly copied into the project's types/ or models/ directory. We recommend fine-tuning type names and package names to match project naming conventions.
Completely free, no registration or login required. Open the page and use immediately, with no feature restrictions, watermarks or forced login.
The tool has no strict file size limit, but conversion speed and rendering performance depend on browser and device performance. We recommend processing JSON within 1MB for best experience; for very large JSON, process first with a JSON formatter or splitting tool.
No. The tool distinguishes integers and floats: integer fields generate int64, float fields generate float64. If you prefer more specific types like int, uint64, replace manually after generation.
Left input box is empty or contains only whitespace. Please paste valid JSON, or click 'Example' to load data, or drag and drop .json/.txt files.
Click the 'Fix JSON' button below the input box; the tool will automatically attempt to fix trailing commas, single quotes, missing quotes, JS-style objects and other issues. After fixing, modified results will be highlighted.
The tool generates PascalCase field names from JSON keys. If keys contain Chinese or special characters, escaped field names may be generated. We recommend changing JSON keys to English lowercase or snake_case, then uniformly adjusting after generation.
Since null cannot infer a specific type, the tool uses interface{} as fallback. You can give the field an example value (e.g. "" or 0) in the source JSON, regenerate, then change the type to a concrete type like string/int64.
Browser rendering of very large JSON and many structs consumes significant memory. Recommendations: ① Keep only example JSON with key fields; ② Split into multiple objects and convert separately; ③ Close other memory-intensive tabs.
When arrays are empty [] or element types are inconsistent, the tool uses interface{} as fallback. Add example elements of the same type, or manually change the type to a concrete slice type like []string, []int64 after generation.
This tool automatically infers corresponding Go types based on JSON value types; common mappings are as follows:
| JSON Value Example | Generated Go Type | Description |
|---|---|---|
"hello" | string | String direct mapping |
true / false | bool | Boolean value |
42 | int64 | Number without decimal point |
3.14 | float64 | Number with decimal point |
null | interface{} | Safe fallback when specific type cannot be inferred |
["a","b"] | []string | String array |
[1,2,3] | []int64 | Integer array |
[{...},{...}] | []UserItem | Object array, element type generated recursively |
{"id":1} | User | Object generates independent struct |
The tool defaults to int64 and float64; adjust as needed in actual projects:
| Scenario | Recommended Type | Reason |
|---|---|---|
| Regular integer IDs, counters | int64 | Matches tool default, compatible with most JSON numbers |
| Database auto-increment primary keys known to be positive | uint64 | Avoids negative numbers, clearer semantics |
| 32-bit systems or explicitly small-range integers | int32 | Reduces memory usage |
| Prices, coordinates, scientific computing | float64 | Matches tool default, standard floating-point precision |
| Precise currency calculations | decimal.Decimal / int | float64 has precision risks; recommend using shopspring/decimal or int in cents |
All processing of this JSON to Go tool completes entirely locally in your browser: JSON parsing, type inference, Go code generation all execute on the client side via Web Worker. Input JSON content, uploaded files and generated Go code are never uploaded to any server, nor recorded, cached or stored in the cloud. After closing or refreshing the page, all input and output content is automatically cleared from memory; only localStorage retains your recent input history (clearable anytime). Suitable for processing JSON containing API keys, tokens, and sensitive business data.