JSON to Go struct converter
Paste JSON and get Go type declarations you can use with encoding/json. Keys become exported field names, every field gets a json tag, nested objects become their own structs, and array items are merged into one element type.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Paste a JSON document, preferably a real response with arrays of several items.
- Pick where
omitempty should be added and whether null values should use pointers.
- Copy the structs into your Go package and add the
package line yourself.
Types
| In the sample |
In Go |
"text" |
string |
7 |
int64 |
2.5 |
float64 |
true |
bool |
[...] |
[]T |
{...} |
a named struct |
mixed or only null |
any |
Example
{"user_id":1,"price":1.5}
gives
type Root struct {
UserID int64 `json:"user_id"`
Price float64 `json:"price"`
}
Tips
The top-level type is called Root; rename it as needed. A nested object is named after its key, and array items use a naive singular, so orders gives Order. Clashing names get a number.
Frequently asked questions
How are field names made?
The key is split at separators and at lower-to-upper case changes, each part is capitalised and the parts are joined, so user_id becomes UserID. Common initialisms such as ID, URL, API, HTTP, JSON, HTML and IP are written in capitals. A name that would start with a digit gets an N in front.
Which number type is used?
A value without a fractional part becomes int64, any other number becomes float64. If the same place holds both, for example [1, 2.5], it becomes float64. A very large integer in the sample may already have lost precision before the tool sees it.
What does omitempty do here?
With "Optional keys", the tag gets ,omitempty on fields that are missing from some of the merged objects. "All fields" adds it everywhere, and "Never" leaves it out. Note that omitempty also drops zero values such as 0 and false when you marshal.
What happens to null and mixed types?
With the pointer option, a place that is sometimes null becomes a pointer, such as *string. A place that is only null, or that holds different types such as a string and a number, becomes any. An empty array becomes []any.
Can I trust the types for every response?
They describe only the sample you pasted. Include several array items and the awkward cases, then review the result before using it.