Skip to content
Code2026-08-282 min read

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:

  1. Open JSON to Go.
  2. Paste your JSON (object or array).
  3. Configure:
    • Generate json tags (on by default);
    • Field naming style (camelCase / PascalCase);
    • Root type name (e.g. Response, User).
  4. Click convert—get complete struct code, copy straight into your project.
  5. 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.Time and 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.


Advertisement