JSON to Go Struct - Go構造体タイプ生成 オンライン
JSONを貼り付けるだけでGo struct定義を即座に生成。PascalCaseネーミング、jsonタグ、型推論 — 100%ブラウザ処理、サーバー送信なし。
よくある質問
JSON to Go structコンバーターとは?
JSON to Go structコンバーターは、JSONデータからGoのstruct型定義を自動生成するツールです。フィールド名、型、json structタグを手動で書く代わりに、サンプルJSONオブジェクトを貼り付けるだけで、すべてのフィールドの正しいGo型を推論します。ネストされたオブジェクトは別のstructとして、配列はスライスとして処理し、マーシャリング/アンマーシャリング用の適切な`json:"field_name"`structタグを生成します。
JSONからGo structsを生成するには?
左側の入力フィールドにJSONデータを貼り付けるだけです。ツールが即座に右側の出力パネルにGo struct定義を生成します。ルートstruct名の設定、structタグのomitemptyトグル、インラインまたは分離ネストstruct の選択、nullableフィールドのポインタ型有効化などで出力をカスタマイズできます。
Go structタグのomitemptyとは?
json structタグの`omitempty`オプションは、GoのJSONエンコーダーにフィールドがゼロ値(空文字列、0、false、nilポインタ、空スライス、空マップ)の場合、出力からフィールドを省略するよう指示します。明示的に設定されたフィールドのみを送信したいAPIリクエストに便利です。
インラインと分離structのどちらを使うべき?
分離struct(デフォルト)は、各ネストされたオブジェクトに名前付き型を作成し、コードベース全体で再利用可能にします。インラインstructは型定義を直接埋め込み、よりコンパクトですが再利用できません。本番コードには分離structを、クイックプロトタイピングにはインラインstructを使用してください。
このツールを使用する際、JSONデータは安全ですか?
はい、完全に安全です。このツールはクライアントサイドJavaScriptを使用してブラウザですべてを処理します。JSONデータがコンピュータから離れることはありません。サーバーアップロード、API呼び出し、データ保存はありません。
Goの命名規則はどのように処理されますか?
ツールはEffective GoのGoの公式命名規則に従います。JSONフィールド名はエクスポートstructフィールド用にPascalCaseに変換されます(例:'user_name'は'UserName'になります)。一般的な略語は適切に大文字化されます:'id'は'ID'、'url'は'URL'、'http'は'HTTP'になります。
nullableフィールドにポインタ型をいつ使うべき?
ゼロ値と不在/null値を区別する必要がある場合にポインタ型を使用します。ポインタ型(*int)では、不在のフィールドはnilになります。nullを使用してオプションまたは不在のデータを示すAPIで作業する場合に「ポインタ型」オプションを有効にしてください。
コード例
// 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"`
// }