JSON to Go Struct
Paste a JSON sample and get Go struct definitions that parse it cleanly with encoding/json. Fields are exported (CamelCase), tagged with the original JSON keys, promoted to pointers when a value might be null, and nested objects get their own named struct declarations.
How to convert JSON to Go struct
-
1
Paste the JSON
A single sample is enough. Multiple samples improve nullable inference.
-
2
Pick naming
PascalCase for exported fields is the Go convention. Choose abbreviation style (`URL` vs `Url`, `ID` vs `Id`).
-
3
Pick optional strategy
Use pointers for nullable fields (most idiomatic), or `omitempty` tags.
-
4
Copy the structs
One root struct plus nested ones. Paste into a `.go` file and hit `go build`.
Example output
For:
{ "first_name": "Alice", "age": 30, "email": null, "tags": ["admin"] }
Output:
type User struct {
FirstName string `json:"first_name"`
Age int `json:"age"`
Email *string `json:"email"`
Tags []string `json:"tags"`
}
Type mapping
| JSON value | Go type |
|---|---|
| string | string |
| integer | int or int64 |
| number (decimal) | float64 |
| boolean | bool |
| null (with non-null sibling) | *T (pointer) |
| array | []T |
| object | Named struct |
| mixed array | []interface{} |
Naming idioms the generator follows
- Initialisms uppercased:
id->ID,url->URL,api_key->APIKey. This matches Go’s recommended style (andgo vet/staticcheckrules). - Snake_case JSON -> PascalCase fields:
first_name->FirstName. - Single-word acronyms preserved:
IPstaysIP,HTTPstaysHTTP. - JSON tag:
json:"first_name"keeps the original key so encoding round-trips cleanly.
Optional fields: pointer or omitempty?
| Strategy | Use when |
|---|---|
Pointer (*T) |
You need to distinguish “absent” from “zero value” |
omitempty |
You only want to skip zero values on marshal; unmarshal stores zero for missing |
| Neither | The field is always present and zero-values are meaningful |
The generator defaults to pointer + omitempty for anything seen as null; tune based on your semantics.
Common mistakes
- Using
intwhere the JSON integer exceedsMaxInt32on a 32-bit target. Useint64for known-large values. - Marshaling back and losing key order.
encoding/jsonemits keys in field-declaration order, not original JSON order. Sort manually if you need canonical output. - Mixed-shape arrays.
[]interface{}loses type safety. Consider union types with atypediscriminator field instead. - Forgetting
omitemptyon optionals. Without it, optional fields serialize as"foo": nullrather than being omitted.
Frequently Asked Questions
Use pointers when you need to distinguish “field absent” from “field is zero”. Use omitempty when the zero value is not a legal business value ("", 0, false always mean “skip this”). Many teams pick one and apply it consistently across a codebase.
Each nested object becomes a separate named struct declaration (e.g. User -> Address). Types are inlined only when the nested struct is tiny and used once; otherwise you get clean, reusable types.
Yes. id becomes ID, url becomes URL, api_key becomes APIKey. This is what go vet expects and what most Go style guides recommend.
The generator emits plain structs with standard json: tags. For custom UnmarshalJSON implementations (e.g. for dates in non-ISO formats), add them manually after generation.
Related Tools
ASCII Table Reference
Full ASCII table from 0 to 127 with decimal, hex, octal, binary, standard names and HTML numeric-reference notation, including NUL, LF and DEL.
Color Palette Generator
Generate monochromatic, analogous, complementary, triadic or tetradic color palettes from a base HEX color and export copy-ready CSS variables.
HTML Character Reference
Searchable list of HTML entities, their named and numeric codes, and a one-click copy for special characters and symbols.
FPS Counter
Measure browser FPS with requestAnimationFrame, smoothing, min/max frame rate, warnings and an optional graph. Runs locally with no upload or API.
HEX Color Picker
Pick or enter a HEX colour and get RGB, HSL, approximate CMYK, relative luminance and contrast ratios against white and black.
JSON Formatter
Paste JSON to pretty-print with 2 or 4 spaces, minify it to compact output, or run a quick syntax check before copying the result.
Tool available in other languages
- JSON เป็นโครงสร้างข้อมูล Go [TH]
- JSON vers Go Struct [FR]
- JSON을 Go 구조체로 변환 [KO]
- JSON ke Struktur Go [ID]
- JSON para Estrutura Go [PT]
- JSON naar Go-structuur [NL]
- JSON إلى بنية Go [AR]
- JSON zu Go Struct [DE]
- JSON a Estructura Go [ES]
- JSON till Go-struktur [SV]
- JSON thành cấu trúc [VI]
- JSON→Go構造体 [JA]
- JSON na strukturę Go [PL]
- JSON 至Go结构 [ZH]
- JSON in Struttura Go [IT]
- JSON в структуру данных [RU]
- JSON'dan Go Yapısına [TR]