JSON to Go Struct

Infer Go structs from JSON.

FreeNo accountOn your device

Runs in your browser. How to verify

Loading tool…

About this tool

Paste a JSON object and get a Go struct with json struct tags, ready for encoding/json. Strings, integers, floats, booleans and slices are mapped. Nested objects fall back to map[string]interface{}.

How to use

1
Step 1
Paste a sample JSON object.
2
Step 2
Press Run.
3
Step 3
Copy the struct and rename Root to suit your package.

How it works

Each key becomes an exported field, made by converted to PascalCase (common initialisms like ID and URL are upper-cased), with a json tag that holds the original key. Types are string, int, float64, bool and []T (from the merged array items); anything nested is map[string]interface{}. The struct is named Root.

Worked example

A scored item

Input
{"id":1,"name":"Ada","score":9.5,"tags":["a"],"active":true}
Output
type Root struct { ID int `json:"id"` Name string `json:"name"` Score float64 `json:"score"` Tags []string `json:"tags"` Active bool `json:"active"` }
Walkthrough
Field names are PascalCase and common initialisms are upper-cased, so id becomes ID. Run gofmt to align the columns.

Limitations

  • No nested structs: nested objects use map[string]interface{}.
  • Field names are PascalCase with common initialisms upper-cased (user_id becomes UserID).
  • No omitempty, pointers or time.Time are generated.

Questions

How do I convert JSON to a Go struct?
Paste a sample object and press Run. Copy the struct and adjust names.
Does it add json tags?
Yes, every field has a json:"key" tag matching the original key.
Why is the field called Id instead of ID?
The tool only capitalises the first letter. Rename it if you follow Go initialism conventions.
How are nested objects handled?
As map[string]interface{}. Write a dedicated struct if you want typed access.

Features

  • json tags
  • int vs float64 detection
  • Slices from arrays
  • Runs in your browser

Related resources

Guides and videos from around the web—opens on YouTube or the publisher’s site.

  • Learn JSON - Full Crash Course for Beginners

    freeCodeCamp.org

    Solid JSON syntax and nesting basics for formatter and validator tools.

    Watch on YouTube
  • What Is A RESTful API?

    Traversy Media

    Most APIs exchange JSON—context for pretty-print and schema checks.

    Watch on YouTube