JSON to Go Struct - Generate Go Types Online
Paste JSON and instantly generate Go struct definitions with proper naming, json tags, and type inference — 100% in your browser.
Frequently Asked Questions
What is a JSON to Go struct converter?
A JSON to Go struct converter is a tool that automatically generates Go struct type definitions from JSON data. Instead of manually writing struct definitions with field names, types, and json struct tags, you paste a sample JSON object and the tool infers the correct Go types for every field. It handles nested objects as separate structs, arrays as slices, and generates proper `json:"field_name"` struct tags for marshaling and unmarshaling.
How do I generate Go structs from JSON?
Simply paste your JSON data into the input field on the left side. The tool will instantly generate Go struct definitions in the output panel on the right. You can customize the output by setting the root struct name, toggling omitempty on struct tags, choosing between inline or separate nested structs, and enabling pointer types for nullable fields. Once satisfied, click 'Copy' to copy the generated code to your clipboard.
What does omitempty do in Go struct tags?
The `omitempty` option in a json struct tag tells Go's JSON encoder to omit the field from the output if it has a zero value (empty string, 0, false, nil pointer, empty slice, or empty map). For example, a field tagged `json:"name,omitempty"` will not appear in the JSON output if the name is an empty string. This is useful for API requests where you only want to send fields that have been explicitly set.
Should I use inline or separate structs?
Separate structs (the default) create a named type for each nested object, making them reusable across your codebase. For example, if your JSON has a 'user' object with an 'address' sub-object, the tool creates both a 'User' struct and an 'Address' struct. Inline structs embed the type definition directly, which is more compact but cannot be reused. Use separate structs for production code and inline structs for quick prototyping or one-off scripts.
Is my JSON data secure when using this tool?
Yes, completely. This tool processes everything in your browser using client-side JavaScript. Your JSON data never leaves your computer - there are no server uploads, no API calls, and no data storage. This makes it safe to use with sensitive API responses, proprietary data schemas, internal microservice contracts, and confidential configuration files.
How does the tool handle Go naming conventions?
The tool follows Go's official naming conventions from Effective Go. JSON field names are converted to PascalCase for exported struct fields (e.g., 'user_name' becomes 'UserName'). Common abbreviations are properly capitalized: 'id' becomes 'ID', 'url' becomes 'URL', 'http' becomes 'HTTP', 'api' becomes 'API'. The original JSON key is preserved in the struct tag for correct JSON marshaling.
When should I use pointer types for nullable fields?
Use pointer types when you need to distinguish between a zero value and an absent/null value. For example, without pointers, a missing 'age' field in JSON unmarshals to 0 (the zero value for int), making it impossible to tell if the age was actually 0 or simply not provided. With pointer types (*int), a missing field becomes nil. Enable the 'Pointer types' option when working with APIs that use null to indicate optional or absent data.
Code Examples
// JSON to Go struct generator
function jsonToGoStruct(json, rootName = 'AutoGenerated') {
const structs = [];
const abbreviations = new Set([
'id', 'url', 'http', 'api', 'html', 'json', 'xml', 'sql',
'ssh', 'tcp', 'udp', 'ip', 'dns', 'uid', 'uuid', 'cpu',
]);
function toGoName(key) {
return key.split(/[-_\s]+/).map(word => {
if (abbreviations.has(word.toLowerCase())) {
return word.toUpperCase();
}
return word.charAt(0).toUpperCase() + word.slice(1);
}).join('');
}
function inferType(value, name) {
if (value === null) return 'interface{}';
if (typeof value === 'boolean') return 'bool';
if (typeof value === 'number') {
return Number.isInteger(value) ? 'int' : 'float64';
}
if (typeof value === 'string') return 'string';
if (Array.isArray(value)) {
if (value.length === 0) return '[]interface{}';
return '[]' + inferType(value[0], name + 'Item');
}
if (typeof value === 'object') {
const structName = toGoName(name);
const fields = Object.entries(value).map(([key, val]) => {
const goType = inferType(val, key);
const goName = toGoName(key);
return `\t${goName} ${goType} \`json:"${key}"\``;
});
structs.push(`type ${structName} struct {\n${fields.join('\n')}\n}`);
return structName;
}
return 'interface{}';
}
const parsed = JSON.parse(json);
inferType(parsed, rootName);
return structs.reverse().join('\n\n');
}
// Example usage
const json = '{"user_id": 1, "user_name": "alice", "address": {"city": "NYC"}}';
console.log(jsonToGoStruct(json));
// Output:
// type Address struct {
// City string `json:"city"`
// }
//
// type AutoGenerated struct {
// UserID int `json:"user_id"`
// UserName string `json:"user_name"`
// Address Address `json:"address"`
// }