JSON to Go Structs
Example
Input:
{"name": "Ali", "age": 30}Output:
package main
type Root struct {
Name string `json:"name"`
Age int64 `json:"age"`
}About this converter
Go's encoding/json needs struct shapes declared, and writing them by hand from a sample payload is exactly the chore this automates. Field names are PascalCased (with the original key preserved in the json tag, plus ,omitempty for keys missing from some items of an array input). Types follow Go conventions: whole numbers int64, decimals float64, nested objects extracted as separate named structs so they can be reused. Watch two honest limits: a number that happens to be whole in the sample (30) types as int64 even if the API later sends 30.5 — widen it manually; and single-letter or acronym keys (id, url) PascalCase to Id/Url rather than the idiomatic ID/URL — a thirty-second rename in your editor.
Frequently Asked Questions
Why int64 and not int?
JSON numbers have no fixed width; int64 is the safe default that encoding/json unmarshals whole values into without overflow on 64-bit platforms.
Can it handle arrays of mixed objects?
Yes — keys are merged across items and missing keys get ,omitempty, matching how ragged API payloads behave.