Skip to content

Go Struct to JSON Converter

Convert Go struct definitions to JSON with example values. Parse Go structs with json tags and generate realistic JSON examples for API documentation, testing, and learning Go's json.Marshal behavior.

Category: code
Use Case: API Documentation, Test Data Generation, Learning Go
Privacy: 100% browser-based

Conversion Options

Recommended Settings

Go Struct & JSON Tag Best Practices

  • Only exported fields (uppercase first letter) are marshaled to JSON
  • Use json struct tags to control JSON field names: `json:"customName"`
  • Add omitempty to exclude zero values from JSON output: `json:"field,omitempty"`
  • Use json:"-" tag to completely exclude a field from JSON marshaling
  • time.Time fields marshal to RFC3339 format (ISO 8601) automatically

Type Mapping (Go → JSON)

  • Go int/int64/uint → JSON number (integers), float64 → JSON number (decimals)
  • Go bool → JSON boolean, string → JSON string, nil → JSON null
  • Go slices []Type → JSON arrays, maps map[string]Type → JSON objects
  • time.Time → ISO 8601 timestamp string (e.g., "2024-01-01T00:00:00Z")
  • Nested structs become nested JSON objects with proper field nesting

Pro Tips

  • Unexported fields (lowercase) are automatically excluded from JSON output
  • Generate example JSON to create API documentation, test fixtures, or sample data
  • The tool respects json tags and omitempty flags just like Go's json.Marshal
  • Use this tool to understand how your Go structs will look when marshaled to JSON

Most Popular

Generate example values with prettified JSON output

How It Works

1

Paste your Go struct definition into the input field

2

Toggle conversion options: generate example values and prettify JSON output

3

Click 'Convert to JSON' to generate example JSON from your struct

4

Review the generated JSON output with realistic example values based on field types

5

Copy to clipboard or download as .json file for use in your documentation or tests

100% Private

Files never leave your device. All processing happens locally in your browser.

Lightning Fast

Powered by Pure JavaScript for optimal performance on modern browsers.

Secure

No data collection, no tracking, no sign-up required.

Frequently Asked Questions

Why is my field not appearing in the JSON output?

Only exported fields (starting with uppercase letter) are marshaled to JSON in Go. Fields starting with lowercase letters are unexported and automatically excluded. Also, fields with `json:"-"` tags are explicitly excluded from JSON output.

How are Go types mapped to JSON?

Go primitives map directly: string→string, int/float→number, bool→boolean. Complex types: slices []T become JSON arrays, maps map[string]T become objects, structs become nested objects. time.Time becomes ISO 8601 timestamp. Pointers (*T) and nil map to null.

What are json struct tags and why use them?

JSON tags control field names and behavior during marshaling. Format: `json:"customName,omitempty"`. They let you: rename fields (ID→id), exclude zero values (omitempty), skip fields entirely (json:"-"), and control casing conventions between Go (PascalCase) and JSON (camelCase/snake_case).

What does omitempty do?

The omitempty flag in json tags excludes fields from JSON output when they have zero values: 0 for numbers, false for bools, empty string, nil for pointers/slices/maps, or empty structs. Useful for optional API fields or reducing JSON payload size.

How does the example value generation work?

The tool generates realistic example values based on Go field types: strings become "example", integers become 123, booleans become true, time.Time becomes ISO timestamp, slices get one example element, and maps get one example key-value pair. Nested structs are expanded recursively.