# How to Convert JSON to Go Struct

> Generate Go struct definitions from JSON data with proper field names, types, and json tags for seamless API integration.

- URL: https://www.browserutils.dev/how-to/convert-json-to-go-struct
- Published: 2026-08-30
- Updated: 2026-07-02

---

## Step 1: Paste your JSON

Copy a JSON object or array from an API response or data file and paste it into the input editor.

## Step 2: Configure naming conventions

Choose whether to use exported (capitalized) field names, set the root struct name, and configure tag formats like `json`, `bson`, or `yaml`.

## Step 3: Review the struct definition

Inspect the generated Go struct, checking that field types are correct and nested objects are represented as separate structs.

## Step 4: Copy the Go code

Click copy to grab the struct definitions. Paste them into your Go source file and start unmarshaling JSON data immediately.

Writing Go structs by hand for complex JSON responses is tedious and error-prone. A single typo in a `json` tag means silent data loss during unmarshaling. The [JSON to Go Struct Converter](/tools/json-to-go) generates correct struct definitions from sample JSON, saving you time and preventing tag mismatches that are difficult to debug.

## Understanding JSON-to-Go-struct conversion

Go's `encoding/json` package maps JSON fields to struct fields using struct tags. The tag `json:"user_name"` tells the unmarshaler to map the JSON key `"user_name"` to that struct field. The converter analyzes your JSON sample, infers the Go type for each value, and generates struct definitions with correct tags.

Type inference follows predictable rules: JSON strings become `string`, numbers without decimals become `int`, numbers with decimals become `float64`, booleans become `bool`, null becomes a pointer type, nested objects become separate structs, and arrays become slices. When a JSON field can be null, the converter uses a pointer type like `*string` to distinguish between a missing value and a zero value.

Nested objects are extracted into their own named structs rather than using anonymous inline structs. This makes the code cleaner and allows you to reuse the nested types elsewhere in your codebase.

## Tips and best practices

- **Use a representative JSON sample.** The converter infers types from the values it sees. If a field is `0` in your sample but can be `3.14` in practice, the converter will generate `int` instead of `float64`. Use a sample that includes diverse values.
- **Add `omitempty` where appropriate.** After generating the struct, add `omitempty` to json tags for optional fields. This prevents zero values from appearing in marshaled output when they should be absent.
- **Use pointer types for nullable fields.** If a JSON field can be `null`, the generated pointer type (`*string`, `*int`) lets you distinguish between "field is absent" and "field is zero." This matters for PATCH-style API endpoints.
- **Flatten unnecessary nesting.** If the generated structs have single-field nested types, consider inlining them to keep your code simpler.

## Common issues

- **Number types may be wrong.** JSON does not distinguish integers from floats. If all sample values happen to be whole numbers, the converter generates `int` even if the field can contain decimals. Review numeric fields carefully.
- **Duplicate struct names from repeated patterns.** If your JSON has multiple nested objects with similar structures, the converter may generate structs with conflicting names. Rename them to be more descriptive.
- **Missing fields not captured.** The converter can only generate fields for keys present in your JSON sample. If the API response has optional fields that are absent from your sample, those fields will be missing from the struct.