Youtilities

Popular

All tools

JSON to Go Struct

Generate Go structs with json tags from any JSON. Nested structs named, pointers for nullable values.

Runs in your browser. Nothing is uploaded.

Input

Waiting for JSON

Go
Go types appear here.

What this does

Paste JSON and get Go structs with json tags, ready for encoding/json. Nested objects become their own structs; arrays of objects are merged into one struct.

Types

Whole numbers become int64 and decimals float64. A value that is sometimes null becomes a pointer, such as *string. A key missing from some items gets omitempty. Empty arrays and mixed values become any.

Names

Field names are exported PascalCase versions of the keys; the original key lives in the tag, so first-name works as FirstName. Two objects with the same shape share one struct.

Missing keys, zero values and pointers

When a key is missing from the JSON, encoding/json leaves the field at its zero value: 0, "" or false. A field that is only sometimes missing gets omitempty in its tag, but omitempty only affects encoding; after decoding you still cannot tell a missing count from a real 0. Fields seen as null become pointers such as *int64, which stay nil when the value is null or absent. If telling absent from zero matters for a field that was never null in your sample, change its type to a pointer by hand. Slices are never pointers, because a nil slice already means nothing was sent.

int64, float64 and decode errors

A number becomes int64 only if every sample of it was a whole number. If the real data later sends 1.5, Unmarshal fails with cannot unmarshal number 1.5 into Go struct field. Include a decimal in your sample, or change the type to float64. Large ids are safe in int64 up to about 9.2 × 10^18. Fields typed any receive whatever JSON holds, and numbers inside them arrive as float64.

Field names and initialisms

Go style writes initialisms in capitals, so the tool turns userId into UserID, apiUrl into APIURL and htmlBody into HTMLBody. The json tag keeps the exact original key, so decoding works whatever the Go name is. When the top level is an array, you get a named slice type such as type Root []RootItem; decode into a value of that type with json.Unmarshal(data, &root). Dates stay string; switch a field to time.Time if it holds RFC 3339 timestamps.

All json tools