N
NJHTools

JSON to Go Struct

Paste JSON and get Go structs with json struct tags for every nested object, ready for encoding/json. Free, no signup, and generated in your browser so the payload is never uploaded.

Language

16 lines · processed in your browser, nothing is uploaded

Go output · root.go

Generated code appears here.

Example: JSON to Go

This JSON sample, with the root type named User, produces the Go below using the default options.

Input JSON
{
  "id": 1024,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "active": true,
  "score": 98.5,
  "tags": [
    "admin",
    "beta"
  ],
  "address": {
    "city": "London",
    "postcode": "NW1 6XE"
  }
}
Generated Go
package main

type User struct {
	ID      int64    `json:"id"`
	Name    string   `json:"name"`
	Email   string   `json:"email"`
	Active  bool     `json:"active"`
	Score   float64  `json:"score"`
	Tags    []string `json:"tags"`
	Address Address  `json:"address"`
}

type Address struct {
	City     string `json:"city"`
	Postcode string `json:"postcode"`
}

How to convert JSON to Go

  1. 1Paste a JSON object or array into the input.
  2. 2Select Go as the output language.
  3. 3Name the root struct, for example Order.
  4. 4Adjust options such as the package name if needed.
  5. 5Copy the structs into a .go file and unmarshal with json.Unmarshal.

Struct tags map JSON keys to Go fields

Go fields must be exported (capitalized) for encoding/json to see them, so a key like user_id becomes a field such as UserID with the tag `json:"user_id"`. The tag keeps the wire name exact while the Go name follows Go conventions.

Keep tags when renaming fields. Changing the field name is safe; changing the tag changes what JSON you read and write.

omitempty and pointers for optional fields

`json:"name,omitempty"` drops the key when marshaling if the value is the zero value: empty string, 0, false, nil pointer, or an empty slice or map. On its own it cannot tell "missing" from "zero".

Use a pointer such as *int when you need that distinction: nil means the key was absent or null, while a pointer to 0 means an explicit zero. Plain values are simpler and fine when zero is never a meaningful value.

Numbers, nulls and mixed types

JSON has one number type, so the generator picks int64 or float64 from your sample. If a field is 1 in your sample but 1.5 in production, unmarshaling fails, so check numeric fields against real data.

Fields whose type varies between records may come out as interface{} (any). Replace those with a concrete type or a custom UnmarshalJSON method once you know the real shape.

Frequently Asked Questions

How do I convert JSON to a Go struct?↓

Paste the JSON, choose Go, and copy the result. Each nested object becomes its own struct with json tags matching the original keys.

Should optional fields be pointers in Go?↓

Use a pointer when you must distinguish a missing or null value from the zero value. If zero and missing mean the same thing in your API, a plain value with omitempty is simpler.

How are optional fields detected?↓

Types are inferred from the sample you paste. A field becomes optional only if it is missing from some of the objects, so paste an array with several representative records, or mark the field optional by hand after generating.

Does it handle nested arrays?↓

Yes. Arrays of objects become slices of a named struct, such as []Item, and arrays of arrays become types such as [][]float64.

Is my JSON uploaded to a server?↓

No. The code generator (quicktype) runs entirely in your browser, so the JSON you paste never leaves your device. That makes it safe to use with internal API responses, though you should still strip secrets before sharing generated code.

Last updated: