S SCHEMA.BIZ

Last updated:

JSON to Go Struct Converter

Paste a JSON example and this JSON to Go struct converter writes the Go types for it: exported field names, json tags that keep the original keys, and pointers where a field can be missing or null.

1
1

How This Works

All conversions use JSON Schema as an intermediate representation. Your JSON Example input is first read into a JSON Schema, then converted to Go Struct. What a format can't express is listed under the output. All processing happens in your browser — your schemas are never sent to a server.

JSON ExampleJSON SchemaGo Struct

How to use it

Paste a JSON value on the left, such as a webhook payload or an API response. The Go code appears on the right as you type, in a package called models; Copy it, or Download it as models.go. From switches the input to JSON Schema, TypeScript or GraphQL.

Nothing you paste leaves the browser. The JSON is first read into a JSON Schema by the same inference as the JSON to Schema Generator, and the structs are written from that schema, with the columns aligned the way gofmt aligns them.

Example: a webhook payload

A billing webhook with a nested invoice, one line item and two fields that are null in this delivery:

{
  "id": "evt_1Q2w3E4r5T6y",
  "type": "invoice.paid",
  "created": 1794268800,
  "livemode": false,
  "data": {
    "invoiceId": "in_9Z8y7X",
    "customerEmail": "billing@example.com",
    "amountPaid": 4900,
    "lines": [
      { "description": "Pro plan", "amount": 4900, "periodEnd": "2026-12-10" }
    ],
    "paidAt": "2026-11-10T09:15:00Z",
    "discount": null
  },
  "request": { "id": "req_abc123", "idempotencyKey": null },
  "apiVersion": "2026-10-01"
}

Every key becomes an exported field and keeps its JSON name in the tag. Common initialisms are written in capitals, so invoiceId becomes InvoiceID and apiVersion becomes APIVersion. Whole numbers become int64, including the Unix timestamp in created. paidAt is a date-time, so it becomes time.Time and the file imports time; periodEnd is a date without a time, which time.Time can't unmarshal, so it stays a string. The two fields that are null here become any, because the sample says nothing about their type.

The converter's Go Struct output:

package models

import "time"

type RootDataLine struct {
	Description string `json:"description"`
	Amount      int64  `json:"amount"`
	PeriodEnd   string `json:"periodEnd"`
}

type RootData struct {
	InvoiceID     string         `json:"invoiceId"`
	CustomerEmail string         `json:"customerEmail"`
	AmountPaid    int64          `json:"amountPaid"`
	Lines         []RootDataLine `json:"lines"`
	PaidAt        time.Time      `json:"paidAt"`
	Discount      any            `json:"discount"`
}

type RootRequest struct {
	ID             string `json:"id"`
	IdempotencyKey any    `json:"idempotencyKey"`
}

type Root struct {
	ID         string      `json:"id"`
	Type       string      `json:"type"`
	Created    int64       `json:"created"`
	Livemode   bool        `json:"livemode"`
	Data       RootData    `json:"data"`
	Request    RootRequest `json:"request"`
	APIVersion string      `json:"apiVersion"`
}

Example: optional and nullable fields

A pointer is how a Go struct says a field may be missing or null. Give the converter several records in an array and it can tell which fields are which:

{
  "issues": [
    {
      "number": 101,
      "title": "Fix login redirect",
      "labels": ["bug"],
      "assignee": { "login": "ann" },
      "closed_at": null,
      "html-url": "https://example.com/issues/101"
    },
    {
      "number": 102,
      "title": "Dark mode",
      "labels": [],
      "assignee": null,
      "closed_at": "2026-11-02T08:00:00Z",
      "votes": 4.5,
      "html-url": "https://example.com/issues/102"
    }
  ]
}

assignee and closed_at are in both issues but null in one, so they're pointers without omitempty: the key is always there, sometimes as null. votes is in one issue only, so it's a pointer with omitempty, which leaves it out of the JSON when it's nil. It's a float64 because its value has a decimal point. Slices and maps are never made pointers. The hyphenated key html-url becomes HTMLURL.

The converter's Go Struct output:

package models

import "time"

type RootIssueAssignee struct {
	Login string `json:"login"`
}

type RootIssue struct {
	Number   int64              `json:"number"`
	Title    string             `json:"title"`
	Labels   []string           `json:"labels"`
	Assignee *RootIssueAssignee `json:"assignee"`
	ClosedAt *time.Time         `json:"closed_at"`
	HTMLURL  string             `json:"html-url"`
	Votes    *float64           `json:"votes,omitempty"`
}

type Root struct {
	Issues []RootIssue `json:"issues"`
}

How JSON values become Go types

Strings become string and booleans bool. A number with no fraction in any sample becomes int64; one with a fraction, or integers and decimals mixed, becomes float64. Arrays become slices of their merged item type, an empty array []any, and an empty object map[string]any.

Each object becomes a named struct, named by its path from the root: the data object above is RootData, and the items of its lines are RootDataLine. A root that is an array becomes a named slice type of its items' struct (type Root []…).

Go has no union types. A value that is a number in one sample and a string in another becomes any, and a note under the output says so.

Does the output compile?

Both examples are among the converter's compile tests: the generated file must be gofmt-clean and pass go vet, and a generated Go test unmarshals each sample into the root type. The Go checks run where Go is installed (the project's CI skips them); the TypeScript, Zod and GraphQL checks also run in CI on every pull request.

5 common mistakes

  1. A money field that happened to be whole. "total": 45 gives int64, and unmarshalling 45.5 into it fails. If a value can have a fraction, include a sample that has one, or change the field to float64 (or to integer cents, if you control the API).
  2. Leaving any for fields that were null. A field that is null in every sample becomes any, which compiles but types nothing. Paste a sample where it has a value, or set the type by hand, as a pointer if it can still be null.
  3. Expecting a Unix timestamp to become time.Time. Only RFC 3339 date-time strings become time.Time. Seconds since 1970 are a number in JSON, so they become int64; convert them with time.Unix.
  4. Dereferencing optional fields without a check. Optional and nullable fields are pointers, so a missing votes is nil. Check for nil before using *issue.Votes, or the program panics on the first record without it.
  5. Converting one record. Keys missing from your only sample aren't in the struct at all. Paste an array of several real records, or merge many samples into a schema in the JSON to Schema Generator and convert that schema here.

Frequently Asked Questions

Is this JSON to Go converter free, and is my JSON uploaded?

It's free, with no account. The conversion runs in your browser, and the JSON you paste isn't sent anywhere. The site's usage log records which formats you converted between and a few counts, as the privacy policy describes.

Why are some fields pointers?

A pointer can be nil, which is how the struct tells a missing or null field from a zero value. Optional fields (not in every sample) are pointers with omitempty; fields that are sometimes null are pointers without it. Slices, maps and any are never made pointers.

Can I change the package name?

Not in the tool: the file always starts with package models. Change that line after you copy or download it.

Does it convert a JSON Schema to Go too?

Yes. Set From to JSON Schema. Then a property's description becomes a comment on its field, a $ref to a definition becomes that struct, and structs that refer to each other in a cycle are held by pointer so the types are finite. The Schema Converter page walks through a recursive example.

Other converters