CruxDevCruxDev Tools Prompts
dev Utility • Zero-Server Privacy • 100% Client-Side

JSON → Go Struct Generator

Generate idiomatic Go structs with proper json tags, type inference, nested struct extraction, and optional omitempty.

Overview

Generates a Go struct declaration from a JSON object: each key becomes an exported PascalCase field with an inferred type and a json tag preserving the original key name, and nested objects become their own structs. Integers become int64, decimals float64, strings string, booleans bool, nulls interface{}, and arrays are typed by merging all of their elements. Paste JSON, read the struct, click to copy.

How It Works

Paste a JSON object into the input panel; the struct appears below immediately. Keys are converted from snake_case or any lowercase form to exported PascalCase identifiers, and each field carries a json tag with the original key name so encoding and decoding round-trip with the original names. Arrays become slices typed by merging all their elements (an array of objects becomes one struct, and fields missing from some elements become pointers with omitempty), empty arrays become []interface{}, and a field that is always null becomes interface{}. Each nested object becomes its own struct named after its key, and the root struct is called Root. Click the output to copy the declaration into your project.

Step-by-Step Usage Guide

  1. Paste a JSON object representing the payload you want to model.
  2. Review the generated field types, especially arrays and any interface{} fields.
  3. Add omitempty to tags for fields you do not want serialized when empty.
  4. Copy the struct into your Go project and unmarshal your payload against it.

Technical Specifications & Standards

The generator walks the whole document, infers a type for every value and emits one struct per nested object, named after its key (array keys are singularised, so tags becomes Tag). Array typing merges every element: integers mixed with decimals give []float64, elements of incompatible types give []interface{}, objects in an array are merged into one struct whose fields are optional when some elements lack them, and an empty array becomes []interface{} since no element exists to infer from. A top-level array of objects is accepted too, and the struct describes each element. The int64-versus-float64 split comes from JavaScript's integer check, so 2 produces int64 and 2.5 produces float64; a value written as 2.0 in JSON parses to the integer 2 in JavaScript and will therefore be typed int64, which matters if your real data always has a fractional form, and integers beyond 2^53 become float64. Tags are plain name bindings except for fields missing from some elements of an array of objects, which get a pointer type and ,omitempty; add omitempty yourself to any other field whose empty values should be dropped. Field names are uppercased through a snake-case-to-PascalCase pass, so user_name becomes UserName while the tag keeps the wire name user_name.

Targeted Use Cases

  • Modeling a third-party API response as Go types without writing fields by hand.
  • Bootstrapping request and response structs for a new Go service from example payloads.
  • Comparing inferred types against expected database column types during design.
  • Teaching how json tags bind Go field names to JSON keys.

Notes & Gotchas

  • Rename the generated structs (Root, Tag and so on) to fit your package; each nested object gets its own struct named after its key.
  • Check arrays with heterogeneous or empty contents, since mixed element types fall back to []interface{} and an empty array has nothing to infer from.
  • Add omitempty yourself where empty fields should be omitted from output JSON; the tool adds it only for fields missing from some array elements.
  • Watch 2.0-style numbers: they parse as integers in JavaScript and are typed int64 even if your data intends floats.

Frequently Asked Questions

How are nested objects converted?

Each nested object becomes its own struct, named after its key (singularised for arrays), and the parent field uses that type - as a pointer if the value can be null or is missing from some array elements.

When does the generator add omitempty?

Only for fields that are missing from some elements of an array of objects, which also become pointers. All other tags are plain name bindings; add omitempty by hand where you want empty fields dropped during marshaling.

How does the tool pick between int64 and float64?

By whether the parsed JavaScript number is an integer: 2 becomes int64 and 2.5 becomes float64. Note that JSON 2.0 parses as the integer 2, so it will be typed int64. Integers beyond 2^53 become float64 because JavaScript cannot hold them exactly.

What happens with an empty array or a null value?

An empty array has no element to infer from and becomes []interface{}. A field that is always null becomes interface{}, the only Go type that can hold any JSON value through encoding/json; a null mixed with real values in an array of objects gives a pointer type instead.