JSON to Go Struct Converter

Paste a JSON sample and get Go struct definitions with json tags. Each nested object becomes its own named struct, and the output updates as you type.

// Go structs will appear here

Related Developer Tools

Runs in your browser: your files and text are not uploaded. Free, no account needed.

Last updated: · Reviewed by the QuickTooly Team

How types are inferred

Type Mapping Rules

  • Strings, booleans: string, bool.
  • Numbers: whole numbers become int, numbers with a fraction become float64. A value such as 3.0 is read as a whole number and becomes int.
  • null: interface{}, since the sample does not show the real type.
  • Nested objects: a new struct named after its parent and key, for example RootAddress.
  • Arrays of objects: one ...Item struct built from the keys of every object in the array. A key missing from some items becomes a pointer, such as *int.
  • Mixed arrays: an array of different primitive types becomes []interface{}. An empty array also becomes []interface{}. An array that holds objects together with strings, numbers, booleans or arrays becomes []interface{} as well. null items next to objects are allowed and the slice uses the item struct.

Go field names are the JSON keys in PascalCase: user_id and user-id both become UserId. Other characters, such as the dot in user.id, are dropped the same way, and a name that would start with a digit gets the prefix Field, as in Field1st. The json tag keeps the original key, so decoding still works.

Worked example

Input:

{"id":7,"score":9.5,"nickname":null,"tags":["a",1],
 "address":{"city":"Oslo"},
 "orders":[{"sku":"A1","qty":2},{"sku":"B2"}]}

Output with JSON tags on:

type RootAddress struct {
	City string `json:"city"`
}

type RootOrdersItem struct {
	Sku string `json:"sku"`
	Qty *int `json:"qty"`
}

type Root struct {
	Id       int `json:"id"`
	Score    float64 `json:"score"`
	Nickname interface{} `json:"nickname"`
	Tags     []interface{} `json:"tags"`
	Address  RootAddress `json:"address"`
	Orders   []RootOrdersItem `json:"orders"`
}

qty is a pointer because the second order has no qty. Nested structs are printed before the struct that uses them.

Things to Fix by Hand

  • Run gofmt on the result. Field names are padded but the types and tags are not aligned.
  • Go style writes initialisms in capitals (ID, URL); the generator writes Id and Url.
  • Replace interface{} with the real type once you know it, and add ,omitempty to tags where needed. The generator never adds it.
  • Two keys in one object that differ only in dropped characters, such as user_id and user-id, give the same field name, which Go rejects. Rename one of the fields.
  • Inside an array of objects, a key's type comes from the first object that has it. If later items use another type, the struct will not match them.

JSON to Go Questions

What does the "pointer fields" option do?

It makes every field a pointer, such as *string or *int, so you can tell a missing or null value apart from a zero value after decoding.

What happens if the JSON is an array at the top level?

The tool builds an item struct from all objects in the array and adds type Root []RootItem. An array of plain values becomes a slice type, such as type Root []string.

What happens with a single number or string?

A top-level value that is not an object or array, such as a single number or string, becomes a named type such as type Root int. Invalid JSON shows an error message instead.

Related Tools