>_devtools

JSON to Go Struct Converter

Generate Go structs with json tags from a JSON sample, with a package name and omitempty option.

What this does

Turns a JSON sample into Go struct definitions with json struct tags, ready forencoding/json. Keys are converted to exported Go names (user_idbecomes UserID, with common initialisms such as ID and URL capitalized), and the original key is kept in the tag so marshaling round-trips.

Example

Input

{
  "id": 42,
  "name": "Ada",
  "verified": true,
  "tags": [
    "admin",
    "dev"
  ],
  "orders": [
    {
      "sku": "A1",
      "qty": 2
    },
    {
      "sku": "B2",
      "qty": 1,
      "note": "gift"
    }
  ]
}

Output with default options

package main

type Root struct {
	ID       int64    `json:"id"`
	Name     string   `json:"name"`
	Verified bool     `json:"verified"`
	Tags     []string `json:"tags"`
	Orders   []Order  `json:"orders"`
}

type Order struct {
	Sku  string  `json:"sku"`
	Qty  int64   `json:"qty"`
	Note *string `json:"note,omitempty"`
}

How values are mapped

  • Whole numbers become int64, other numbers float64, strings string, booleans bool.
  • Nested objects become their own named structs; arrays become slices such as []Order.
  • A field missing from some objects in an array becomes a pointer with omitempty (for example *string).
  • ISO 8601 timestamps become time.Time when date detection is on. A value that is always null becomes interface{}.

Options

Set the package name and root type name, change the tag (for example yaml ordb), or add omitempty to every field. Types only keeps just the package clause and structs; turn it off to also generate Unmarshal andMarshal helper functions.

Things to know

The structs reflect the sample you paste, so include several objects when a field is sometimes absent. Arrays with mixed element types produce a wrapper struct with a pointer per possible type; you will usually want to simplify those by hand.

Privacy

Generation runs in a Web Worker in your browser. The JSON is never uploaded.