JSON to Go Converter
Generate Go structs from JSON data. Paste your JSON and configure options:
Generate Go Structs from JSON
This tool converts JSON data into Go struct definitions with proper json tags. It infers types automatically and handles nested objects by generating separate struct types.
Example Output
From this JSON:
{
"id": 1,
"user_name": "john",
"is_active": true
}The converter generates:
package main
type Root struct {
ID int `json:"id,omitempty"`
UserName string `json:"user_name,omitempty"`
IsActive bool `json:"is_active,omitempty"`
}Options Explained
omitempty
Adds ,omitempty to json tags. When marshaling to JSON, fields with zero values (empty string, 0, false) are omitted:
// With omitempty
type User struct {
Name string `json:"name,omitempty"`
}
// {} if Name is empty
// {"name":"John"} if Name has valuePointer types
Uses pointer types for nested structs and optional fields. Helpful for distinguishing between "not set" (nil) and "set to zero value":
type User struct {
Age *int `json:"age,omitempty"`
Profile *Profile `json:"profile,omitempty"`
}Type Mapping
| JSON | Go | Notes |
|---|---|---|
| string | string | |
| integer | int / int64 | int64 for large values |
| float | float64 | |
| boolean | bool | |
| null | interface or pointer | |
| array | []T | Type from first element |
| object | Nested struct |
Using Generated Structs
Unmarshal JSON
import "encoding/json"
jsonData := []byte(`{"id": 1, "user_name": "john"}`)
var user Root
err := json.Unmarshal(jsonData, &user)
if err != nil {
log.Fatal(err)
}
fmt.Println(user.UserName) // "john"Marshal to JSON
user := Root{
ID: 1,
UserName: "john",
IsActive: true,
}
jsonBytes, err := json.Marshal(user)
// {"id":1,"user_name":"john","is_active":true}With HTTP APIs
resp, err := http.Get("https://api.example.com/user")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var user Root
err = json.NewDecoder(resp.Body).Decode(&user)Go Naming Conventions
The converter follows Go conventions:
- Exported fields — PascalCase (capitalized) for JSON access
- JSON tags — Preserve original key names for serialization
- Struct names — PascalCase from JSON key names
Handling Edge Cases
Empty arrays
Empty arrays become []interface since the element type can't be inferred. Update manually to the correct type.
Mixed-type arrays
If an array contains different types, it becomes []interface. Consider restructuring your data or using custom unmarshal logic.
Null values
Null values become interface by default, or pointer types if that option is enabled.
Related Tools
- JSON Validator — Validate JSON before converting
- JSON to Java — Generate Java POJOs
- JSON to Python — Generate Python dataclasses
- JSON to TypeScript — Generate TS interfaces
- JSON Schema Generator — Create schema from data
Frequently Asked Questions
Should I use omitempty?
Use omitempty when you want to exclude zero-value fields from JSON output. Don't use it when zero values are meaningful (e.g., a counter that can be 0).
When should I use pointer types?
Use pointers when you need to distinguish between "field not set" (nil) and "field set to zero value" (0, "", false). Common for optional API fields.
How do I handle custom types?
The generated structs use basic types. For custom types (like time.Time for dates), manually update the struct and implement custom UnmarshalJSON if needed.