JSON to Go Struct Generator
Last updated:
Jsonic's JSON to Go Struct generator converts a JSON object into Go struct type definitions with json struct tags. It maps JSON strings to string, integers to int64, decimals to float64, booleans to bool, and null values to pointer types (*string, *int64, etc.) so they can be nil. Nested JSON objects become separate named struct types declared before the root. JSON arrays become Go slice types ([]string, []int64, []NestedStruct). Field names are converted from snake_case or camelCase to PascalCase, and the original JSON key is preserved in the json struct tag. All conversion runs in your browser with no data upload.
How to convert JSON to a Go struct
- Paste your JSON object into the left panel, or click Example to load a sample.
- Optionally change the root struct name (default: Root).
- Click Generate.
- The right panel shows the Go struct definitions with json tags and correct Go types.
- Copy the code and paste it into your Go project.
FAQ
How are JSON types mapped to Go types?
The generator maps JSON value types to Go types: JSON strings → string, JSON integers (whole numbers) → int64, JSON decimals → float64, JSON booleans → bool, JSON null → pointer type (*string, *int64, etc.), JSON arrays → slice type ([]string, []int64, []StructName), and JSON objects → named struct type.
Why does the generator use int64 instead of int?
JSON numbers can represent values larger than 32-bit integers. Using int64 ensures correctness on all platforms regardless of whether int is 32-bit or 64-bit. For smaller domains, you can manually change int64 to int or int32 after generating.
How are nullable fields handled?
JSON null values map to Go pointer types. For example, a null string field becomes *string, allowing it to be nil in Go. Fields that are absent in some array items but present in others are also typed as pointers with the omitempty tag, allowing json.Unmarshal to leave them nil when missing.
How are JSON field names converted to Go field names?
JSON keys in snake_case (user_id) or camelCase (userId) are converted to PascalCase for Go field names (UserId, UserName). The original JSON key is always preserved in the json struct tag so json.Unmarshal and json.Marshal work correctly.
How are nested objects handled?
Each nested JSON object becomes a separate named Go struct type. Nested structs are declared before the root struct so they can be referenced by name. The root struct then uses the nested struct type by name (e.g., Address Address).
How do I unmarshal JSON into the generated struct?
Use json.Unmarshal([]byte(jsonString), &myStruct) from the encoding/json package. For example: var u Root; json.Unmarshal(data, &u). The generated json tags ensure field names map correctly even if they use snake_case or camelCase in the JSON.
What if my JSON has an array at the root level?
Paste one element from the array as the input to generate the element struct type. Then use []Root (a slice of the struct) to unmarshal the full array: var items []Root; json.Unmarshal(data, &items).
Can I use this for parsing REST API responses in Go?
Yes. Fetch the JSON response from your API, paste it into the tool, and get the struct definition immediately. The generated struct with json tags works directly with json.Unmarshal and the net/http package.