Xerobit

JSON to Go Struct Converter

Paste JSON and get Go structs with json tags. The converter infers types (int64, float64, bool, string, slices, nested structs), merges array elements into one struct, and lets you choose pointer or omitempty for nullable fields. It runs entirely in your browser. This json to go struct tool is free and requires no installation.

A JSON object or array. A root array becomes a slice type of the merged element struct.

Applies to fields that are null or missing in some objects of an array, or null in the sample.

Go Struct

package main

type Response struct {
	UserID   int64    `json:"user_id"`
	Username string   `json:"username"`
	Email    string   `json:"email"`
	IsActive bool     `json:"is_active"`
	Balance  float64  `json:"balance"`
	Tags     []string `json:"tags"`
	Profile  Profile  `json:"profile"`
	Metadata any      `json:"metadata,omitempty"`
}

type Profile struct {
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	AvatarURL string `json:"avatar_url"`
}

By · Published · How we build and test tools

How to use the json to go struct converter

  1. Paste a JSON object or array into the input area.
  2. Choose a root type name and a package name.
  3. Select how nullable fields are written: omitempty adds the tag option, pointer uses *Type.
  4. The Go structs appear instantly. Copy the output. This json to go struct converter updates live as you type. Review the types before using them, because one sample cannot show every value the data may take.

How the json to go struct conversion works

The converter reads the JSON with the built-in JSON.parse and infers a Go type for every value:

  • Whole numbers become int64; numbers with a fraction, or too large for int64, become float64. A field that is an integer in one array element and a fraction in another becomes float64.
  • Booleans become bool and strings become string.
  • Arrays become slices. All elements of an array are merged into one element type, so an array of objects produces one struct that covers every key seen. Arrays mixing different kinds, such as [1, "a"], become []any.
  • Objects become named structs; the struct name comes from the key (a key items gives Item), with the parent name added in front if that name is already taken.
  • null on its own, and empty arrays, become any or []any because the real type cannot be known from the sample.

Field names are converted to Go's exported PascalCase. Words are split at underscores, hyphens, spaces and camelCase boundaries, and common initialisms (ID, URL, URI, HTTP, HTTPS, API, JSON, XML, HTML, CSS, JS, SQL, UUID, IP, TCP, UDP, DNS, SSH, TLS, SSL, JWT, UI, CPU, RAM, RPC and a few more) are kept uppercase. For example user_id becomes UserID and apiURL becomes APIURL. Fields keep the order of the JSON, with tabs and aligned columns as gofmt would write them. The any alias needs Go 1.18 or later; use interface{} on older versions.

Generated struct example

The default input in the tool converts to:

package main type Response struct { UserID int64 `json:"user_id"` Username string `json:"username"` Email string `json:"email"` IsActive bool `json:"is_active"` Balance float64 `json:"balance"` Tags []string `json:"tags"` Profile Profile `json:"profile"` Metadata any `json:"metadata,omitempty"` } type Profile struct { FirstName string `json:"first_name"` LastName string `json:"last_name"` AvatarURL string `json:"avatar_url"` }

With the pointer option, fields that are null or missing in some objects use pointer types instead of omitempty.

Null handling: omitempty vs pointer

Nullability is only visible when a field is null or missing somewhere in the sample, for example in one object of an array. Choose the strategy that matches your API contract.

  • omitempty (default): the field keeps its concrete type and the tag gets ,omitempty. A JSON null decodes to the zero value, and on marshal zero values are left out. Nested structs become pointers (*Profile) because omitempty never skips a struct value.
  • pointer: scalars and structs become pointers (*int64, *string, *Profile). A JSON null or a missing key decodes to nil, so you can tell zero from absent. Fields missing in some objects also get ,omitempty.

A field that is only ever null in the sample is typed any in both modes, since nothing shows what it holds.

Acronym handling reference

The converter splits keys into words and keeps common initialisms uppercase in the Go field name, following the Go style guidance.

JSON keyGo fieldNote
user_idUserIDID is an initialism
apiURLAPIURLcamelCase is split into API + URL
http_statusHTTPStatusHTTP
max_retriesMaxRetriesno initialism
jwt_tokenJWTTokenJWT
ip_addressIPAddressIP
1st_placeField1stPlacea field cannot start with a digit

Root arrays and edge cases

If the JSON root is an array of objects, the converter writes a slice type, for example type Items []ItemsItem, plus one struct for the merged element, so you can decode the array directly with json.Unmarshal. A root array of numbers or strings gives type Items []int64 or []string.

Keys that differ only in case or punctuation, such as user_id and userId, get numbered field names. Keys that cannot appear in a Go struct tag (empty keys, or keys with commas, quotes or backticks) are written with json:"-" and a comment, and the note under the output says so; decode those objects into a map[string]any. Nesting deeper than 100 levels is rejected.

Common mistakes

  • Invalid JSON: trailing commas, single quotes or comments cause a parse error, and the tool shows the parser's message.
  • Treating one sample as the schema: a field that is 5 in your sample may be 5.5 elsewhere. Merging array elements helps, but check the API documentation for the real types.
  • Large numbers: JSON numbers are read as IEEE 754 doubles, so integers beyond 2^53-1 lose precision before the type is chosen. For exact 64-bit identifiers, send them as strings.
  • Duplicate keys: JSON.parse keeps the last value, so the struct has one field.

Worked examples

ExampleInputResult
Simple user objectJSON input: {"id": 42, "name": "Alice", "active": true}; Root struct name: User; Package name: models; Null handling: omitemptypackage models / type User struct { / ID int64 `json:"id"` / Name string `json:"name"` / Active bool `json:"active"` / }
Nested with nulls and arraysJSON input: {"order_id": "ORD-100", "items": [{"sku": "A1", "qty": 2}, {"sku": "B2", "qty": 1}], "discount": null, "metadata": {"source": "web", "campaign_id": 5}}; Root struct name: Order; Package name: orders; Null handling: pointerpackage orders / type Order struct { / OrderID string `json:"order_id"` / Items []Item `json:"items"` / Discount any `json:"discount"` / Metadata Metadata `json:"metadata"` / } / type Item struct { / Sku string `json:"sku"` / Qty int64 `json:"qty"` / } / type Metadata struct { / Source string `json:"source"` / CampaignID int64 `json:"campaign_id"` / }
Acronyms and mixed typesJSON input: {"userID": 1, "apiURL": "https://api.example.com", "httpStatus": 200, "config": {"maxRetries": 3, "timeoutMS": 5000}, "flags": [true, false, true]}; Root struct name: Config; Package name: config; Null handling: omitemptypackage config / type Config struct { / UserID int64 `json:"userID"` / APIURL string `json:"apiURL"` / HTTPStatus int64 `json:"httpStatus"` / Config ConfigConfig `json:"config"` / Flags []bool `json:"flags"` / } / type ConfigConfig struct { / MaxRetries int64 `json:"maxRetries"` / TimeoutMs int64 `json:"timeoutMS"` / }
Root array (wrapped)JSON input: [{"name": "A"}, {"name": "B"}]; Root struct name: Items; Package name: main; Null handling: omitemptypackage main / type Items []ItemsItem / type ItemsItem struct { / Name string `json:"name"` / }

FAQ

Does this tool send my JSON to a server?

No. The conversion runs in your browser using the built-in JSON parser. Nothing is uploaded.

How large a JSON document can I convert?

Up to 2 MB. Larger input is rejected with a message; for very big files use a local command-line generator.

Why does my numeric ID become int64 instead of string?

JSON numbers are numbers, so a whole number becomes int64. If your IDs can exceed 2^53-1, send them as strings in JSON and change the generated field to string.

How are nested struct names generated?

From the key in PascalCase, so profile becomes Profile and an array under items becomes Item. If that name is already used, the parent name is put in front (OrderAddress), and a number is added if there is still a clash.

Can I customize the json tag key (e.g., keep snake_case)?

The tool uses the original JSON key as the tag value, so snake_case keys stay snake_case in the tag. The Go field name is always PascalCase.

What if my JSON has a key like 'type' or 'class'?

Field names are exported PascalCase (Type, Class), so they never collide with Go keywords, which are all lower-case. The lower-case key is kept in the json tag.

Does this json to go struct converter support JSON Schema or OpenAPI?

No. This tool only converts concrete JSON instances. For schema-driven generation, use a dedicated code generator.