Last updated
Example: Nested Objects
// Input JSON:
{
"user": {
"id": 1,
"name": "Alice",
"address": {
"street": "123 Main St",
"city": "New York",
"zip": "10001",
"country": "US"
}
}
}
// Generated Go structs:
type Root struct {
User User `json:"user"`
}
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Address Address `json:"address"`
}
type Address struct {
Street string `json:"street"`
City string `json:"city"`
Zip string `json:"zip"`
Country string `json:"country"`
}
Example: Arrays
// Input JSON:
{
"users": [
{ "id": 1, "name": "Alice", "roles": ["admin", "editor"] },
{ "id": 2, "name": "Bob", "roles": ["viewer"] }
],
"total": 2
}
// Generated Go structs:
type Root struct {
Users []User `json:"users"`
Total int `json:"total"`
}
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Roles []string `json:"roles"`
}
Example: API Response with Optional Fields
// Input JSON:
{
"id": 1,
"username": "alice",
"email": "alice@example.com",
"displayName": "Alice Smith",
"bio": null,
"avatarUrl": "https://example.com/avatar.jpg",
"createdAt": "2024-01-15T14:30:00Z",
"updatedAt": "2024-03-10T09:15:00Z"
}
// Generated Go struct (null fields use pointer types):
type Root struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
DisplayName string `json:"displayName"`
Bio *string `json:"bio"` // pointer — can be null
AvatarURL string `json:"avatarUrl"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
Example: Ethereum API Response
// Input JSON (eth_getBlock response):
{
"number": "0x12a05f2",
"hash": "0xabc123...",
"parentHash": "0xdef456...",
"timestamp": "0x65a4b2c1",
"gasLimit": "0x1c9c380",
"gasUsed": "0x1234567",
"transactions": [
"0xtx1hash...",
"0xtx2hash..."
],
"miner": "0xmineraddress..."
}
// Generated Go struct:
type Block struct {
Number string `json:"number"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Timestamp string `json:"timestamp"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
Transactions []string `json:"transactions"`
Miner string `json:"miner"`
}
Example: Complex Nested with Arrays of Objects
// Input JSON:
{
"order": {
"id": "ORD-001",
"customer": { "id": 1, "name": "Alice" },
"items": [
{
"product": { "id": 10, "name": "Widget", "price": 9.99 },
"quantity": 2,
"discount": 0.1
}
],
"total": 17.98,
"status": "pending"
}
}
// Generated Go structs:
type Root struct {
Order Order `json:"order"`
}
type Order struct {
ID string `json:"id"`
Customer Customer `json:"customer"`
Items []Item `json:"items"`
Total float64 `json:"total"`
Status string `json:"status"`
}
type Customer struct {
ID int `json:"id"`
Name string `json:"name"`
}
type Item struct {
Product Product `json:"product"`
Quantity int `json:"quantity"`
Discount float64 `json:"discount"`
}
type Product struct {
ID int `json:"id"`
Name string `json:"name"`
Price float64 `json:"price"`
}
Example: Using the Generated Struct
// Generated struct for a user API response:
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
// Unmarshal JSON into struct:
package main
import (
"encoding/json"
"fmt"
)
func main() {
data := []byte(`{"id":1,"name":"Alice","email":"alice@example.com"}`)
var user User
if err := json.Unmarshal(data, &user); err != nil {
panic(err)
}
fmt.Println(user.Name) // → Alice
fmt.Println(user.Email) // → alice@example.com
}
// Marshal struct back to JSON:
jsonBytes, _ := json.Marshal(user)
fmt.Println(string(jsonBytes))
// → {"id":1,"name":"Alice","email":"alice@example.com"}
omitempty Tag Option
// Without omitempty (zero values are included in JSON output):
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Bio string `json:"bio"`
}
// json.Marshal({ID: 1, Name: "Alice"}) → {"id":1,"name":"Alice","bio":""}
// With omitempty (zero values are omitted from JSON output):
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Bio string `json:"bio,omitempty"`
}
// json.Marshal({ID: 1, Name: "Alice"}) → {"id":1,"name":"Alice"}
Paste your JSON to generate Go structs instantly. The converter handles nested objects, arrays, null values, and snake_case to PascalCase name conversion automatically.
Example: Simple Object
// Input JSON:
{
"id": 42,
"name": "Alice Smith",
"email": "alice@example.com",
"age": 30,
"active": true,
"score": 98.5
}
// Generated Go struct:
type Root struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
Active bool `json:"active"`
Score float64 `json:"score"`
}