JSON to TypeScript, Go, Python, PHP, Rust
Private by design — runs entirely in your browser
Inferring the right type definitions from a real API response is one of those tasks that should take five seconds but somehow eats fifteen minutes. This tool reads a JSON sample in your browser, walks the structure, and emits idiomatic types for TypeScript, Go, Python, PHP, or Rust — with optional fields, ISO date detection, and proper integer-vs-float distinction.
How the inference works
The tool walks the JSON tree once, building an intermediate representation: primitives become string / int / float / bool / null, arrays infer the element type from the union of their members, and objects produce a struct whose field types come from the values. When the same field appears in some array elements but not others, it is marked optional so the generated type can decode partial data without exploding. Numbers without a fractional part are emitted as int64 (Go) / int (Python) / i64 (Rust) and numbers with one as float64 / float / f64. Strings that match ISO 8601 (`2024-03-15T10:30:00Z`) become Date / time.Time / datetime / DateTimeInterface / chrono::DateTime.
Per-language output style
Each emitter follows the conventions of its target language. TypeScript: exported interfaces with optional `?:` syntax. Go: structs with `json:` tags, optional fields as pointers with `omitempty`. Python: `@dataclass` with required fields first and `Optional[T] = None` last (matching the dataclass ordering rule). PHP: final classes with strict typing and `@var list<T>` phpdoc hints for typed arrays. Rust: `#[derive(Serialize, Deserialize)]` structs with `Option<T>` for nullable fields and `#[serde(rename)]` whenever the snake_case Rust name differs from the JSON key.
When to use it (and when not to)
This generator is meant for the first 80% of the work: getting a working type definition from a sample payload in seconds. It does not replace a hand-written schema when you need precise constraints (regex on strings, min/max on numbers, discriminated unions). For those cases, take the generated output as a starting point and tighten the types. The detection of optional fields is heuristic — based on what is present in your sample — so always pass several diverse samples through if you can.
Frequently Asked Questions
- Does the JSON I paste leave my browser?
- No. The parser, type inference, and code emission all run in JavaScript inside your tab. Open the Network panel while you generate — you will see zero outbound requests. Safe for production payloads with real tokens and PII.
- Why is my field marked optional when it always has a value?
- The optional flag is set when an array contains some objects with the field and some without. If you paste a single object, no field will be marked optional. If you paste an array of identical objects, no field will be marked optional either. The flag triggers only when the structure inside the array varies.
- How are integer vs float distinguished?
- JSON itself has only one numeric type. The tool inspects each value and emits int when the value has no fractional part (`42`, `1000`) and float when it does (`3.14`, `99.0`). If an array mixes the two, the type widens to float to avoid lossy decoding.
- Can it detect ISO date strings?
- Yes. Strings matching `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM:SS[.sss][Z|±HH:MM]` are emitted as the language's native datetime type (Date in TS, time.Time in Go, datetime in Python, DateTimeInterface in PHP, chrono::DateTime in Rust). Other date formats are kept as strings — paste-test if you are unsure.
- Do I need to install anything to use the generated code?
- TypeScript output is plain — no dependencies. Go output uses the standard library only (time.Time when ISO dates are detected). Python output uses the standard dataclasses module. PHP output uses native types. Rust output requires serde + chrono if you have ISO dates. The header / imports are written into the output where applicable.
- Can I generate types for an array of objects at the root?
- Yes. If the root value is an array of objects, the tool emits a struct for the element and a top-level type alias for the array. For example, a JSON file with `[{...}, {...}]` named `Users` produces a `User` interface plus `type Users = User[];` in TypeScript, or `pub type Users = Vec<User>;` in Rust.