JSON to Go Struct

Generate Go structs with json tags from a JSON sample.

Runs on your deviceFreeNo sign-up
Runs in your browser
Loading tool…

Designed to run in your browser. See the privacy policy.

About JSON to Go Struct

This JSON to Go converter turns a sample JSON document into Go struct definitions with json tags, ready to use with encoding/json. Paste an API response or config file and get a typed starting point instead of writing structs field by field.

Field names are converted to exported PascalCase and follow the usual Go initialism style, so user_id becomes UserID and image_url becomes ImageURL. Each field keeps its original JSON key in a tag such as json:"user_id". Nested objects become their own named structs, and the Root type is listed first so the output reads top-down. The conversion is processed on your device as you type.

How to use

  1. Paste a JSON object or array into the JSON input box.
  2. Read the generated code in the Go structs panel; it refreshes on every edit.
  3. If an error appears, fix the invalid JSON it points to.
  4. Click copy to grab the structs, or download to save them as types.go.
  5. Rename the Root struct and adjust any types to fit your package.

FAQ

Which Go types does it use for numbers?+
Whole numbers in the sample become int64 and numbers with a fractional part become float64. If a field can hold both, change it to float64 yourself, since the type is decided from the sample value.
How are null values and empty arrays typed?+
A null field becomes interface{} and an empty array becomes []interface{}, because the sample gives no clue about the real type. When objects in an array are merged, a non-null sample is preferred where one exists.
Does it add omitempty or pointer types?+
No. Tags contain only the JSON key, and fields are plain values rather than pointers. Add ,omitempty or switch to pointers by hand where you need to tell a missing field from a zero value.
How does it handle arrays of objects?+
All objects in the array are merged into one struct that has the union of their keys, and the field becomes a slice of that struct. For arrays of scalars or arrays, the element type comes from the first element only.
What if the top-level JSON is an array?+
An array of objects produces a Root struct for one element plus a RootList slice type. An array of scalars produces a single slice type, and a plain top-level value produces a one-line type such as type Root string.