JSON to Go: Differences, Edge Cases & Pitfalls
Generate Go structs from JSON with json tags, omitempty choices and notes on where float64 damages large integers.
Go structs need exported fields to be serialised by encoding/json, which is why property names and json tags come in pairs. Numbers landing as float64 is inherent to Go decoding; anything that must be an integer needs a manual change to int64.
Format Differences
| Aspect | JSON | Go struct |
|---|---|---|
| Field visibility | Not applicable | Lowercase fields are not serialised; must be exported |
| Key mapping | Any key name | Explicit json:"snake_case" tags |
| Numbers | One number type | Choose float64 / int / int64 by hand |
| null | null value | Pointers, sql.NullString, or omitempty |
| Missing fields | Key absent | Zero value (0, "", nil mean different things) |
| Nesting | Nested objects | Nested structs or map[string]interface{} |
| Arrays | Native | Slices []T, homogeneous element type |
| Runtime cost | Cheap | Reflection decoding is slower; codegen speeds it up |
Edge Cases
- With omitempty you cannot distinguish zero from unset, so use pointers when "present and zero" matters.
- An empty array decodes to a nil slice and marshals back as null instead of [], which can change the contract.
- Keys with hyphens or dots are not identifiers, so json:"..." tags are the only mapping.
- Very long integers (order IDs) lose precision as float64; use string or int64.
Common Pitfalls
- Manually lowercasing fields to "look idiomatic" makes serialisation emit an empty object with no error.
- Money as float64 accumulates cent-level error, discovered during reconciliation.
- An empty array round-trips to null and the front end throws while iterating.
- Sprinkling omitempty strips explicitly-set zero values and loses business meaning.
Related Tools
- JSON to Go StructConvert sample JSON to Go structs with json tags, nested types and omitempty option for typing API clients, config loaders and data model definitions.
- Data Format ConverterConvert between JSON, YAML, TOML, XML and CSV in one place with a single paste and instant preview for seamless multi-format config and data conversion.
- JSON to YAMLConvert JSON data to YAML format with custom indentation and nested object support. Turn verbose JSON configs into clean readable YAML in a couple of seconds.
- Excel to JSONConvert Excel files (xlsx, xls, csv) to JSON format. Supports nested headers, sheet selection and key mapping. Choose the sheet and header row and keep numbers.