JSON to Go structs

Paste JSON and get Go structs with correct json tags and exported field names. Runs in your browser; nothing is uploaded.

About this converter

Go's encoding/json needs a struct to unmarshal into, and writing one by hand for a large API response is exactly the kind of mechanical work worth automating. The awkward part is not the types, it is the tags: Go requires exported field names starting with a capital letter, while JSON keys are almost always lowercase or snake_case, so every single field needs a struct tag to bridge the two.

This generator writes those tags for you. Each field gets an exported PascalCase name and a json tag carrying the original key exactly as it appeared, which is the part people most often get subtly wrong by hand - a mistyped tag produces a zero value at runtime with no error anywhere.

Nested objects become their own named struct types referenced by pointer, rather than being embedded anonymously. Pointers rather than values because a nil pointer can represent an absent object, which a zero-valued struct cannot distinguish from an object that was genuinely all zeros.

Fields that are missing from some records in an array get omitempty on their tag, so marshalling back out does not emit keys that were not there originally. Whole numbers become int64 and decimals become float64 - int64 rather than int because JSON numbers from other languages routinely exceed what a 32-bit int holds on some platforms.

The usual caveat about inference applies: this reads the sample you paste, not a schema. A field null in every record you have cannot be typed and becomes interface{}. Optionality is inferred from which records omitted a key, so paste the full response rather than one element if you want that to be right.

The generator runs in your browser, so a real API response with real data never leaves your machine.

Frequently asked questions

Why are nested structs pointers?
A nil pointer distinguishes an absent object from one whose fields all happen to be zero. With a value type those two cases are indistinguishable after unmarshalling.
Why int64 rather than int?
JSON numbers produced by other languages frequently exceed 32 bits, and int is platform-dependent. int64 is the safe default; narrow it yourself if you know the range.
What does omitempty do here?
It is added to fields missing from some records, so marshalling back to JSON does not emit keys that were absent in the original.

Related converters