Switch tool

JSON to Types

Infer type definitions from JSON for TypeScript, Go, Java and Kotlin — auto-names nested types, merges object shapes across array items, and marks nullable fields.

Target
JSON input
344 chars · 17 lines
TypeScript interfaces
441 chars · 27 lines

How to use

Purpose

JSON-to-types generator producing TypeScript / Java / Kotlin / Swift / Go / Rust / Python / Dart definitions. Infers field types from JSON data, generates interface/class/struct definitions. Smart nested object handling (multiple types generated), array element inference, null handling (optional properties), same-shape merging (avoid duplicates). Common for frontend-backend type alignment, third-party API onboarding, TS strict mode + JSON data. All generation runs locally.

Steps

  1. Paste JSON (object or array) on the left
  2. Pick target language: TypeScript / Java / Kotlin / Swift / Go / Rust / Python / Dart
  3. Right pane shows type definitions
  4. Type name: based on root — Root / RootItem or custom
  5. Nested objects: auto-generate sub-types and reference them
  6. Array element inference: [1,2,3] → number[], [{a:1}] → A[]
  7. Optional: null-valued fields → ?: / nullable
  8. TS options: interface vs type alias, JSDoc comments

FAQ

How are null fields inferred?
JSON has no type concept — pure data inference. null with no more info: unknown / any / Any. Field sometimes has value, sometimes null: Type | null (TS), Optional<Type> (Java 8+), nullable (Kotlin). Best: give the tool multiple JSON samples to merge-infer more accurately.
What if array elements have inconsistent types?
All same: [1,2,3] → number[]. Mixed: [1,"a",true] → (number | string | boolean)[] (TS union). Object array with different properties: [{a:1},{a:1,b:2}] merged to {a: number, b?: number}[] (b optional).
TypeScript interface vs type alias?
Mostly equivalent. Differences: 1) interface supports declaration merging (same-name interfaces auto-merge), type does not; 2) type can define unions (A | B) and primitive aliases (type Email = string); 3) interface better for extensible object types, type better for complex compositions. Use interface for object shapes, type for complex types.
Should generated Java/Kotlin use Lombok?
Java default emits full getter/setter/toString/equals (verbose). Check "Lombok" to simplify to @Data. Kotlin uses data class by default (one-liner, no Lombok needed). Swift uses struct (built-in, no annotation needed).
How are field name styles handled?
JSON commonly uses camelCase (frontend) or snake_case (Python/Go backend). TypeScript: preserve original (camelCase TS idiom). Java/Kotlin: convert to camelCase. Python: convert to snake_case + @field annotation to preserve original. Go: convert to PascalCase + struct tag `json:"original"`. Tool follows target language convention.

Use cases

  • Frontend-backend alignment: backend JSON response → TS types in one click
  • Third-party API onboarding: doc example JSON → target language types
  • TS strict mode: replace any with inferred specific types
  • Cross-language communication: define JSON Schema and generate types in multiple languages
  • Rapid prototyping: paste draft JSON, get scaffold code

Use cases

Frontend-backend alignment, third-party API, TS strictness, cross-language communication, rapid prototyping. Frontend, backend, mobile devs. Multi-language, nested auto-split, null smart handling, naming convention are the differentiators.