1. Home
  2. Developer tools
  3. JSON to Go Struct

JSON to Go Struct

Generate the structs you need to unmarshal an API response in Go, with idiomatic names and tags that keep the original JSON keys.

Free, no sign-up. Runs in your browser: nothing is uploaded.

How to use JSON to Go Struct

  1. Paste JSON.
  2. Set the package and root type names.
  3. Copy the structs into your .go file.

Features

  • Idiomatic names: user_id → UserID, html_url → HTMLURL
  • json:"key,omitempty" tags for optional and null fields
  • Pointers for optional values, so missing differs from zero
  • int64, float64, bool, string, []T and any
  • Optional Go time values for ISO 8601 timestamps

Supported formats

You can open

  • JSON
  • JSON Lines
  • Typed or pasted text

You get

  • GO
  • Copied text

Private by design

Your files are opened and processed by your browser on this device. They are never uploaded to openWorkstand or anyone else, so the JSON to Go Struct is safe for contracts, IDs, photos and work documents. After you have used it once while online, it also works without an internet connection. How local processing works

Good to know

  • Types are inferred from the samples given; paste several varied samples for accurate optional fields.

Guide

Generating Go structs from JSON

Go’s encoding/json maps JSON onto structs, which means every API you call needs a set of struct definitions with the right types and tags. Writing them by hand for a large response is tedious, and it is easy to choose the wrong number type or forget which fields can be missing. Paste a sample response here and get idiomatic structs you can drop into a package.

Idiomatic names

Go exports fields that start with a capital letter, so every JSON key becomes a PascalCase field name. Common initialisms follow Go style rather than plain capitalisation: id becomes ID, url becomes URL, html_url becomes HTMLURL and order_id becomes OrderID, which is what golint and most Go codebases expect. If two keys would end up with the same name, the second gets a number.

Struct tags

Because Go names differ from the JSON keys, every field carries a tag with the original key: json:"order_id". Tags also control output when you marshal the struct back to JSON. Fields that were missing or null in at least one sample get ,omitempty, so encoding leaves them out instead of writing a zero value.

Pointers or zero values

In Go, a missing int field decodes as 0 and a missing string as “”, which you cannot tell apart from a real zero or empty string. By default, optional and nullable fields become pointers — *string, *int64, *Address — so nil means absent. Switch pointers off for simpler code when the difference does not matter to you. Slices and any are never pointers, because nil already means absent for them.

Choosing the number types

Whole numbers become int64 and decimals float64, safe for any value JSON can carry. If a field holds whole numbers in some samples and decimals in others, it becomes float64. Narrow types by hand where you know the range, for example int for a page count.

Dates and unknown values

Switch on date types and ISO 8601 timestamps become time.Time, which encoding/json parses from RFC 3339 strings; the time import is added for you. Values that are always null, or that change type between samples, become any, the alias for interface{}.

A worked example

From the sample user record, the generator writes four structs: User, Order, Item and Address. The order’s key order_id becomes OrderID int64 \json:“order_id”`, the coupon that appears in only one order becomes Coupon *string `json:“coupon,omitempty”`, and the address’s always-null unitbecomesUnit any`.

After generating

Run gofmt to align the tags in your editor’s style, rename the root type to something meaningful, and add the package to your module. If the API publishes an OpenAPI document, compare the structs against it and tighten any field that the samples happened to leave empty.

Arrays at the root and JSON Lines

If the response is an array, the generated struct describes one element; decode into a slice with var users []User. Logs and exports in JSON Lines format can be pasted as they are, one document per line: all lines are merged into the same structs, which is a quick way to find the fields that only appear now and then.

Generate TypeScript types for the frontend with JSON to TypeScript, structs for another backend with JSON to Rust, Go request code with cURL to Code, or inspect the input with the JSON Formatter.

Checked against the current version of the JSON to Go Struct on .

Troubleshooting

The file does not open

Check that it is one of the supported formats above and opens in another app. Damaged files and password-protected PDFs cannot be read until they are repaired or unlocked.

It is slow or stops on a very large file

Everything runs on your device, so speed depends on its memory. Close other tabs, use a computer rather than a phone, or split the file into smaller parts first.

The download did not start

Look for your browser’s download bar or icon. Some browsers ask once before a site may download files; allow it and click the button again.

Frequently asked questions

How do I decode into these structs?

Use the encoding/json package: pass the body and a pointer to a User to its Unmarshal function. For a response stream, create a Decoder on the body and call Decode, which avoids reading the whole body into memory first.

Why does a field show up as any?

It was null in every sample or held different types in different samples, so no single Go type fits. Replace any with the real type once you know it, or with RawMessage from encoding/json to decode it later.

Can I reject unknown keys?

Yes. Create a Decoder and call its DisallowUnknownFields method before Decode. By default encoding/json silently ignores keys the struct does not declare.

Why are some fields pointers?

A plain int is 0 when the key is missing, which you cannot tell apart from a real 0. A *int64 is nil when the key is absent or null. Switch pointers off if you do not need that distinction.

Why int64 rather than int?

JSON numbers can exceed 32 bits, and int64 has the same size on every platform. Change it to int if your values are small and you prefer the shorter type.

Keep working with the result

openWorkstand also includes a free workspace with documents, spreadsheets, tasks, projects and a calendar that save to a folder on your computer.