{ JSON to Go Struct }

// convert json samples into typed go struct definitions

Convert JSON samples into typed Go struct definitions with exported fields, json tags, and nested types. Free, instant, browser-based.

🐹

Go struct will appear here

Paste JSON and click Generate

HOW TO USE

  1. 01
    Paste JSON

    Copy your JSON sample — an API response, config file, or any JSON object — into the input area.

  2. 02
    Configure Options

    Set the root struct name, enable omitempty for optional fields, or toggle pointer types for nullable values.

  3. 03
    Copy Output

    Click Generate, then copy the Go struct definitions. Paste directly into your .go files — ready to use with json.Unmarshal.

FEATURES

Nested structs Exported fields json tags Array types Type inference omitempty Pointer fields Null handling

USE CASES

  • 🔧 Deserializing REST API responses in Go
  • 🔧 Defining config file structs for Go apps
  • 🔧 Creating models for encoding/json
  • 🔧 Generating types for database query results
  • 🔧 Building Go SDK clients from JSON schemas

WHAT IS THIS?

This tool converts any valid JSON sample into Go struct type definitions. It inspects the keys and values, infers the correct Go types (string, int, float64, bool, nested structs, slices), and outputs idiomatic Go code with proper json:"..." struct tags and exported (PascalCase) field names.

RELATED TOOLS

FREQUENTLY ASKED QUESTIONS

How does type inference work?

The tool inspects each JSON value at runtime. Numbers without decimals become int, numbers with decimals become float64, booleans map to bool, strings stay string, JSON objects become nested structs, arrays become slices, and null becomes interface{} or a pointer type when the pointer option is enabled.

What does the "omitempty" option do?

When enabled, each struct tag gets the ,omitempty directive appended: json:"field_name,omitempty". This tells encoding/json to skip the field during marshaling if it holds the zero value (empty string, 0, false, nil, empty slice, etc.), which is useful for optional API fields.

Can I use the output directly in my Go project?

Yes. The generated code is valid Go and uses only the standard library. Just paste it into a .go file, ensure the package declaration matches, and use json.Unmarshal(data, &Root{}) to decode your JSON. No third-party dependencies required.

How are JSON keys converted to Go field names?

JSON keys are converted to PascalCase (exported) Go identifiers. For example, user_id becomes UserID, firstName becomes FirstName, and http_status becomes HTTPStatus. Common abbreviations like ID, URL, HTTP, JSON, API, and UUID are uppercased per Go convention.

What happens with arrays of mixed types?

If an array contains elements of the same type, the slice type is inferred (e.g., []string, []int). If the array contains mixed types or mixed objects, the type falls back to []interface{}. Arrays of objects produce a named struct type and a slice of that type.

What does the "Pointer fields" option do?

When enabled, all non-primitive fields (and fields whose value is null) are wrapped in pointers (e.g., *string, *int, *NestedStruct). This is useful when working with APIs that can return null for any field, allowing you to distinguish between a missing field and a zero value.

Does this support deeply nested JSON?

Yes. The generator recurses through all nested objects and arrays, creating a separate named struct for each nested object level. Each inner struct is named based on its parent field name in PascalCase (e.g., a field "address" containing an object produces an Address struct).

Is my JSON data sent to a server?

No. All conversion happens entirely in your browser using JavaScript. Your JSON data never leaves your machine and is not sent to any server. This makes the tool safe to use with sensitive or proprietary API responses.

What Is a JSON to Go Struct Converter?

A JSON to Go Struct converter is a developer tool that reads a JSON sample and automatically generates the corresponding Go type definitions. Instead of manually writing type MyStruct struct { ... } declarations for every field, you paste your JSON, click generate, and get complete, idiomatic Go code you can drop directly into your project.

Go's encoding/json package requires struct types with exported (capitalized) field names and json:"..." struct tags to correctly unmarshal JSON data. Writing these by hand for complex, deeply nested API responses is tedious and error-prone. This tool automates the entire process.

💡 Looking for premium web development assets? MonsterONE offers unlimited downloads of templates, UI kits, and assets — worth checking out.

How Go JSON Struct Tags Work

Go uses struct tags — string literals after field declarations — to control JSON marshaling and unmarshaling behavior. The encoding/json package reads the json key in the struct tag to map between JSON field names and Go field names. For example:

type User struct {
    ID        int    `json:"id"`
    FirstName string `json:"first_name"`
    Email     string `json:"email,omitempty"`
}

Without struct tags, Go uses the field name directly, which means FirstName would only match a JSON key called FirstName (case-insensitive, but only for simple cases). Tags make the mapping explicit and reliable, especially for snake_case JSON keys from APIs.

Go Type Inference from JSON Values

The converter inspects each value in your JSON sample and maps it to the appropriate Go type:

This type inference is a best-effort based on the sample provided. If your API can return different types for the same field in different responses (a rare but real scenario), you may need to manually adjust the output to use interface{} or a custom JSON unmarshaler.

PascalCase Field Names and Go Conventions

Go requires struct fields to be exported (begin with an uppercase letter) to be accessible by the encoding/json package. The converter automatically transforms JSON key names — which are typically camelCase or snake_case — into idiomatic PascalCase Go field names.

The conversion also handles common abbreviations per the official Go naming conventions. Keys like user_id, api_key, http_status, and url become UserID, APIKey, HTTPStatus, and URL respectively — matching the Go standard library style rather than producing awkward names like UserId or Url.

Handling Nested Objects and Arrays

Real-world JSON from APIs rarely consists of flat key-value pairs. It almost always includes nested objects (user profiles with addresses, orders with line items) and arrays of objects. This converter handles both recursively.

Each nested JSON object becomes a separate named struct definition. The struct name is derived from the field name in the parent object. For example, if your JSON has an "address" field containing an object, the tool generates an Address struct and uses it as the field type in the parent. This keeps the output clean and reusable — the Address struct can be referenced independently anywhere in your code.

Arrays of objects follow the same pattern: an array of address objects produces both an Address struct and a []Address field type. Arrays of primitives produce typed slices like []string or []int.

The omitempty Option

The omitempty option appends ,omitempty to all struct tags. When marshaling (converting Go structs back to JSON), this directive tells the encoder to omit fields that hold their zero value: empty strings, zero integers, false booleans, nil pointers, and empty slices. This is extremely useful when building API clients where optional request fields shouldn't be sent if not set — keeping your JSON payloads clean and API-compliant.

Pointer Fields for Nullable Values

In Go, there is no built-in "nullable" concept for primitive types. A string field can never be null — it will simply be an empty string. To represent truly nullable fields (where you need to distinguish between "field was not present" and "field was present with a zero value"), you use pointer types: *string, *int, *bool.

When the pointer fields option is enabled, the converter wraps all field types in pointers. After unmarshaling, you check if field != nil to determine whether the JSON contained the field. This is the standard Go approach for optional API fields and is commonly used in SDKs and API clients.

Integrating Generated Structs into Your Go Project

The generated output is standard Go code. To use it:

  1. Copy the generated struct definitions.
  2. Paste them into an appropriate .go file in your project (create a new types.go or models.go if needed).
  3. Add the correct package declaration at the top: package main or your package name.
  4. Use json.Unmarshal([]byte(jsonData), &Root{}) to decode your JSON into the struct.
  5. Run go vet ./... to verify the generated code is valid.

No additional imports are needed beyond encoding/json from the standard library, which is already available in every Go project.

☕