JSON to Go Converter

Generate Go structs from JSON data. Paste your JSON and configure options:

Loading...
Loading...

Generate Go Structs from JSON

This tool converts JSON data into Go struct definitions with proper json tags. It infers types automatically and handles nested objects by generating separate struct types.

Example Output

From this JSON:

{
  "id": 1,
  "user_name": "john",
  "is_active": true
}

The converter generates:

package main

type Root struct {
	ID       int    `json:"id,omitempty"`
	UserName string `json:"user_name,omitempty"`
	IsActive bool   `json:"is_active,omitempty"`
}

Options Explained

omitempty

Adds ,omitempty to json tags. When marshaling to JSON, fields with zero values (empty string, 0, false) are omitted:

// With omitempty
type User struct {
    Name string `json:"name,omitempty"`
}

// {} if Name is empty
// {"name":"John"} if Name has value

Pointer types

Uses pointer types for nested structs and optional fields. Helpful for distinguishing between "not set" (nil) and "set to zero value":

type User struct {
    Age     *int     `json:"age,omitempty"`
    Profile *Profile `json:"profile,omitempty"`
}

Type Mapping

JSONGoNotes
stringstring
integerint / int64int64 for large values
floatfloat64
booleanbool
nullinterface or pointer
array[]TType from first element
objectNested struct

Using Generated Structs

Unmarshal JSON

import "encoding/json"

jsonData := []byte(`{"id": 1, "user_name": "john"}`)
var user Root
err := json.Unmarshal(jsonData, &user)
if err != nil {
    log.Fatal(err)
}
fmt.Println(user.UserName) // "john"

Marshal to JSON

user := Root{
    ID:       1,
    UserName: "john",
    IsActive: true,
}
jsonBytes, err := json.Marshal(user)
// {"id":1,"user_name":"john","is_active":true}

With HTTP APIs

resp, err := http.Get("https://api.example.com/user")
if err != nil {
    log.Fatal(err)
}
defer resp.Body.Close()

var user Root
err = json.NewDecoder(resp.Body).Decode(&user)

Go Naming Conventions

The converter follows Go conventions:

  • Exported fields — PascalCase (capitalized) for JSON access
  • JSON tags — Preserve original key names for serialization
  • Struct names — PascalCase from JSON key names

Handling Edge Cases

Empty arrays

Empty arrays become []interface since the element type can't be inferred. Update manually to the correct type.

Mixed-type arrays

If an array contains different types, it becomes []interface. Consider restructuring your data or using custom unmarshal logic.

Null values

Null values become interface by default, or pointer types if that option is enabled.

Related Tools

Frequently Asked Questions

Should I use omitempty?

Use omitempty when you want to exclude zero-value fields from JSON output. Don't use it when zero values are meaningful (e.g., a counter that can be 0).

When should I use pointer types?

Use pointers when you need to distinguish between "field not set" (nil) and "field set to zero value" (0, "", false). Common for optional API fields.

How do I handle custom types?

The generated structs use basic types. For custom types (like time.Time for dates), manually update the struct and implement custom UnmarshalJSON if needed.