toolqik

138 · JSON

JSON to Go

Generate Go structs with json tags from example JSON, with omitempty and pointers.

Runs in your browser
Integers
Options
Go code

  

About this tool

Go’s encoding/json needs a struct with a json:"…" tag on every field. Paste a JSON example and this generator writes the structs: exported field names in Go style, tags that map back to the original keys, slices for arrays, and a named type for each nested object.

Example

{ "id": 7, "user_url": "https://…", "tags": ["a"], "manager": null }

type Root struct {
	ID      int64    `json:"id"`
	UserURL string   `json:"user_url"`
	Tags    []string `json:"tags"`
	Manager any      `json:"manager"`
}

Go conventions applied

  • Names: keys become exported PascalCase fields, and common initialisms are capitalized as Go style requires: ID, URL, API, HTTP, JSON, UUID.
  • omitempty is added to fields missing from some samples, so they are left out when marshalling instead of being sent as zero values.
  • Pointers for nullable fields: a *string can tell “null or missing” apart from an empty string. Turn this off if zero values are fine for you.
  • Integers default to int64 so large IDs fit on every platform; choose int if you prefer. Decimals become float64. Slices don’t need pointers: a nil slice already means null.
  • Mixed or unknown values become any (an alias for interface{}, Go 1.18+).

How types are inferred

  • Every object becomes its own named type. Names come from the key (address → Address), and items of an array get the singular (orders → Order).
  • For arrays of objects, all items are merged into one type: a field missing from some items becomes optional, and a field that is sometimes null becomes nullable.
  • Integers and decimals in the same field merge to a decimal type. Fields with mixed types (say a number in one item and a string in another) fall back to the language’s “any” type.
  • Identical nested shapes are reused instead of generating duplicates.

The code is generated from examples, so it can only be as accurate as the sample. Paste a response that includes optional fields and nulls (or several items) for the best result, then check the types against the API documentation. Generated in your browser: your JSON is not uploaded.

Decode JSON into the structs

var root Root
if err := json.Unmarshal(body, &root); err != nil {
	return err
}

// Reject unexpected fields:
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
err := dec.Decode(&root)

FAQ

How do I convert JSON to a Go struct?

Paste the JSON above. A struct is generated for the root and every nested object, with json tags that map each field to its original key.

Why are some fields pointers?

Pointers let you tell null or missing apart from a zero value such as "" or 0. Fields that were null in the sample become pointers; turn the option off to use plain types.

What does omitempty do?

When marshalling, a field with omitempty is left out if it has its zero value. The generator adds it to fields that were missing from some samples.

Why is it ID and not Id?

Go style capitalizes initialisms consistently, so the generator writes ID, URL, API and similar. The json tag keeps the original key, so decoding still works.

How are arrays of mixed types handled?

They become []any. You can replace any with a concrete type or json.RawMessage to decode later.

esc
↑↓ navigate↵ open