TXT · Text & Data tools

JSON to Go Struct Converter

Top-level type
Package
Field tags (comma-separated)
JSON input
Go output
Inference
GolanguageJSON sampleinput0types0lines out0chars out
Go to the full JSON to Types generator with all ten languages

Shareable link

Settings are written to the URL as you change them. Nothing differs from the defaults yet.

Go structs from a JSON sample

Paste a response body and get structs that encoding/json decodes into without a field left unset. Keys become exported PascalCase field names, each with a json:"key" tag pointing back at the original.

Example

{ "id": 7, "name": "Ada", "orders": [{ "sku": "X1", "qty": 2 }, { "sku": "X2", "qty": 1, "gift": true }] }
type Customer struct {
	ID     int64   `json:"id"`
	Name   string  `json:"name"`
	Orders []Order `json:"orders"`
}

type Order struct {
	Sku  string `json:"sku"`
	Qty  int64  `json:"qty"`
	Gift *bool  `json:"gift,omitempty"`
}

Gift is a pointer because only one order carries it; omitempty on it is added when the omitempty option is checked. ID follows Go’s initialism convention.

Options

Package sets the package line. Types only strips the helper functions. Field tags takes a comma-separated list such as json,yaml or json,db when the same struct is read by more than one encoder. omitempty appends ,omitempty to every optional field’s tag.

Limits

Go has no union type, so a field that mixes strings and numbers in the sample becomes a small struct with one pointer member per type and its own UnmarshalJSON/MarshalJSON. A field that is always null becomes interface{}. Repeated string values become a string-typed Status type with constants when enum inference is on. ISO 8601 strings become time.Time when Dates from ISO strings is on, and the file imports time.

Frequently Asked Questions

One exported struct per object shape, with exported field names and a json tag carrying the original key. Unless Types only is checked, Unmarshal and Marshal helper functions for the top-level type follow.

A key missing from some objects becomes a pointer field, such as *bool, so a missing value decodes as nil. Check omitempty to add ,omitempty to those tags so nil pointers are left out when encoding.

Whole numbers become int64 and anything with a fraction becomes float64. A field with both across the sample becomes float64.

A field that is always null becomes interface{} so any JSON value decodes into it. A field that mixes types, such as numbers and strings, becomes a struct with one pointer per type and custom UnmarshalJSON and MarshalJSON methods.

Yes. Field tags is a comma-separated list; json,yaml emits both a json and a yaml tag on every field.

Generate types for one language

Full JSON to Types Generator tool

Explore Our Tools

Browse all tools