JSON to Go
Convert JSON to Go structs online with json tags, pointers for null values and omitempty for optional keys. Paste an API response and copy the types.
Something not working, or missing a feature? Send feedback
About the JSON to Go
The JSON to Go converter turns a sample API response into Go structs with json tags, so you can decode it with encoding/json straight away. Field names follow Go conventions (ID, URL, APIKey), nested objects become their own structs, and keys that are missing from some array items get omitempty.
Conversion happens in your browser, so your files and records stay private.
How to use the JSON to Go
- Paste a JSON sample, ideally a real response with every field filled in.
- Set the name of the root struct.
- Copy the structs into your package and decode with json.Unmarshal.
Example
Input
{
"id": 7,
"fullName": "Grace Hopper",
"email": null,
"roles": ["admin"],
"team": { "name": "Compilers", "size": 4 },
"projects": [
{ "name": "COBOL", "year": 1959 },
{ "name": "A-0", "year": 1952, "archived": true }
]
}Result
type User struct {
ID int64 `json:"id"`
FullName string `json:"fullName"`
Email any `json:"email"`
Roles []string `json:"roles"`
Team Team `json:"team"`
Projects []Project `json:"projects"`
}
type Project struct {
Name string `json:"name"`
Year int64 `json:"year"`
Archived bool `json:"archived,omitempty"`
}
type Team struct {
Name string `json:"name"`
Size int64 `json:"size"`
}Keys missing from some array items (archived) get omitempty, and initialisms follow Go style (ID, not Id). email was only ever null, so its type is unknown (any): paste a sample where it has a value to get *string.
Common mistakes
- Optional booleans and numbers. With omitempty, a missing archived and archived: false both decode to false. If the difference matters, change the field to *bool.
- A sample with nulls. A field that is only ever null becomes any. Generate from a response where every field has a value, then add pointers for the ones that can be null.
Works well with the JSON to Go
- JSON Formatter: tidy and check the JSON before generating types.
- JSON to TypeScript: generate matching TypeScript interfaces for the front end.
- JSON Validator: find syntax errors in the sample first.
Frequently asked questions
Why are some fields pointers?
A field that was null in the sample becomes a pointer (*string, *int64), so nil can represent null. Slices and maps can already be nil, so they stay as they are.
Why is a field typed as any?
Its value was always null, or it held different kinds of values (for example a number in one item and a string in another), so the converter cannot pick one type. Use a sample where the field has a real value.
Which integer and float types are used?
Whole numbers become int64 and numbers with a fraction become float64. If a field holds both, it becomes float64. Change them to int or float32 if you know the range.
Related tools
JSON to CSV
Convert JSON arrays to CSV online with automatic column detection, nested object flattening and custom delimiters. Open the result in Excel or Sheets.
CSV to JSON
Convert CSV to a JSON array online with header detection, type inference and delimiter auto-detection. Turn spreadsheet exports into data for APIs and code.
JSON to XML
Convert JSON to well-formed XML online with a configurable root element and attribute support. Validate the input and download the XML file.
XML to JSON
Convert XML to JSON online, keeping attributes and turning repeated elements into arrays. Malformed XML is reported with its location.
JSON to YAML
Convert JSON to clean, human-readable YAML online. Turn API responses and package files into config for Kubernetes, Docker Compose or CI pipelines.
YAML to JSON
Convert YAML documents to formatted JSON online with syntax validation. Errors are reported by line, so broken indentation is quick to find and fix.