Skip to content

JSON to Go Struct

Generate Go structs from JSON.

Input
Output

No result

About JSON to Go Struct

JSON to Go Struct turns a sample JSON object into a Go struct with exported field names, correct json tags, and Go types inferred from each value. Strings, booleans, numbers and arrays of those map cleanly, and the field-name capitalisation needed for a struct field to be exported is applied for you.

The json tags are the part that earns its keep. Go matches field names to JSON keys by tag rather than by identifier, so a key written in snake case would not line up with a capitalised field name without one. Generating the tags correctly is what lets you decode a real payload without renaming anything on either side.

Two mappings need attention before the struct is useful. Every number becomes a floating-point type, including values that were plainly integers — so identifiers, counts and monetary amounts should be changed to an integer type or, for money, to a fixed-point representation rather than left as a binary float. And nested objects collapse to the empty interface type, because a single sample gives no name for the structure inside. A large payload therefore needs its nested objects promoted to their own structs by hand.

How to use JSON to Go Struct

  1. 1 Paste a JSON object that is representative of the real payload.
  2. 2 Generate the struct and check that each json tag matches the wire key exactly.
  3. 3 Replace floating-point types with integers where the values are whole numbers.
  4. 4 Promote the fields typed as the empty interface into named structs of their own.

Frequently asked questions

Why are my integers typed as floating point?

Because the type inference works from the JSON representation, where there is no integer type — only a number. Any field whose values are always whole should be changed to an integer type, and any field holding money should not use a binary float at all.

What happened to my nested objects?

They are mapped to the empty interface type, since a nested structure with no name cannot be expressed as a typed field from one sample. The keys inside them are not lost from your data, but the struct will need a named type defined for them before the payload can be used type-safely.

Does it handle arrays of objects?

An array is typed from its first element, so an array of strings becomes a string slice. An array of nested objects still lands on the interface type, and the element struct has to be defined separately.

Will the field names still decode correctly after I edit the types?

Yes, as long as you leave the json tags untouched. Go associates a field with a JSON key through the tag, so the identifier may be renamed freely while the wire format keeps working.

Free, online, and private

Free to use

JSON to Go Struct is completely free. There is no account to create, no login, and no hidden charge or usage limit.

Runs online in your browser

It is a free online tool that runs entirely in your browser. Nothing to download or install — open the page and start using it right away.

Your data stays private

All processing happens locally on your device. Your input is never uploaded, stored, or tracked, so it never leaves your browser.

How JSON to Go Struct behaves in detail

These are the specifics of this implementation — the exact ranges, defaults and fixed settings you are working with.

Parameter Value
Output shape a single type Root struct, each field carrying a json:"key" tag with the original key
Type mapping string to string, number to float64, boolean to bool, while null and nested objects become interface{}
Arrays element type taken from the first element — []string, []float64, or []interface{} when the array is empty
Field naming the cap() helper removes an underscore and upper-cases the next letter, then upper-cases the first letter
Root requirement the parsed document must be an object, because Object.entries is called on it directly

Compiled from the implementation of this tool and checked against it by itoolnet.

What JSON to Go Struct does not do

Knowing the edges of a tool saves more time than another list of its features. These are the cases where it will not do what you might expect.

  • Nested objects are not given their own struct. Any object-valued key is typed interface{}, so the generated code compiles but none of the sub-fields can be reached without a type assertion or a hand-written struct.
  • Every number becomes float64, integers and identifiers included. There is no int64 and no json.Number option, so a large numeric id is typed as a float and can lose precision when the value is written back out.
  • Field names are only cleaned up for underscores. A key containing a space, a hyphen or a dot is copied in unchanged — user-id becomes User-id — so the emitted struct will not compile until those fields are renamed by hand.
  • Array element types come from the first element only, so a mixed array is mistyped and an empty one degrades to []interface{}, with nothing in the output warning that the shape could not be determined.

Tested and documented by itoolnet, the team that builds and maintains this site.

Share & embed this tool

Writing a guide or article? You are welcome to link to JSON to Go Struct with the snippet below. It is free, runs entirely in your browser, and needs no signup.