Integrating a third-party API returns a big chunk of JSON. You don't want to hand-write a Go struct with dozens of fields—slow and error-prone with tags. Can you auto-convert?
Yes. Here are the type-mapping rules for JSON → Go struct, and how to generate code with json tags in one click.
JSON type to Go type mapping
| JSON type | Go type | Notes |
|---|---|---|
| Object {} | struct {…} | Nested struct |
| Array [] | slice []T | Element type inferred from content |
| String "a" | string | |
| Number (no decimal) | int64 / float64 | Depends on decimal point |
| Number (decimal) | float64 | |
| Boolean | bool | |
| null | pointer or interface{} | Use *T to tell "zero" from "unset" |
| Mixed array | []interface{} | Avoid if possible |
Key pitfall: numbers and null
Go has no "nullable primitive". If a field may be null, writing int will error on decode—use a pointer:
type User struct {
Name string `json:"name"`
Age *int `json:"age"` // may be null
Score *float64 `json:"score"`
}
Step-by-step generation (no plugin)
Use this site's JSON to Go tool—runs locally, JSON never uploaded:
- Open JSON to Go.
- Paste your JSON (object or array).
- Configure:
- Generate
jsontags (on by default); - Field naming style (camelCase / PascalCase);
- Root type name (e.g.
Response,User).
- Generate
- Click convert—get complete
structcode, copy straight into your project. - Need other languages? Use JSON to TypeScript or JSON to Java.
Full example
Input JSON:
{
"id": 1024,
"name": "Alice",
"isActive": true,
"tags": ["go", "api"],
"profile": {
"age": 30,
"city": "Shanghai"
}
}
Generated Go:
type Profile struct {
Age int `json:"age"`
City string `json:"city"`
}
type Response struct {
ID int `json:"id"`
Name string `json:"name"`
IsActive bool `json:"isActive"`
Tags []string `json:"tags"`
Profile Profile `json:"profile"`
}
Advanced tips
- Format first: tidy messy JSON with the JSON formatter for more accurate inference.
- Distinguish zero vs missing: use pointers for external API fields, so "not returned" isn't mistaken for "0 / empty".
- Time fields: JSON times are usually RFC3339 strings; use
time.Timeand ensure tag/format align. - Exported fields: Go fields must be capitalized to be serialized by
encoding/json—the tool handles this; don't miss it when writing by hand.
FAQ
Q: Array elements have inconsistent types?
Use []interface{} and assert yourself. Best to have the API return a uniform structure.
Q: Number sometimes int, sometimes decimal?
float64 is safest; if definitely integer, change to int64 manually.
Q: Too deeply nested, too many structs?
Use map[string]interface{} to receive temporarily, then define structs as needed, or convert in layers.
Q: Generate omitempty tags?
Depends. For request bodies, omitempty drops zero-value fields; for responses, usually skip it to avoid treating "missing" as "zero".
Summary
JSON to Go struct = generate tagged structs by type mapping; use pointers for null, pick number types carefully. Use this site's JSON to Go tool to generate locally—JSON not uploaded, safer for sensitive payloads.
Related Tools
Related Articles
How to Convert JSON to Java Entity Class (with annotations and List nesting)
Got JSON API data and want the matching Java POJO? Learn JSON-to-Java type mapping and common annotations, and step-by-step how to generate serializable classes locally.
Complete Guide to HTML to JSX Conversion for React Developers
Understand the key differences between HTML and JSX, master className, style objects, camelCase attributes, and quickly migrate HTML snippets into React projects.
Regex Not Matching? Troubleshoot These 6 Common Pitfalls
How to troubleshoot when a regex doesn't match or matching fails? This article walks through 6 common pitfalls — greedy quantifiers, missing flags, unescaped special chars, lookaround assertions, newline handling, and Unicode properties — with before/after examples to help you quickly locate regex debugging issues.