Skip to main content

JSON to Code

Generate type definitions from JSON objects. Infers structure and produces idiomatic interfaces, dataclasses, structs, or types for your target language.

Overview

The JSON to Code tool analyzes a JSON value and generates:
  • TypeScript — Interfaces with proper type annotations
  • Python — Dataclasses with type hints
  • Go — Structs with json tags
  • Rust — Structs with Serde derives
Supports nested objects, arrays, and primitive types. Field names are sanitized to valid identifiers for each language.

Use Cases

API client generation

Generate types from API responses for type-safe client code

Schema scaffolding

Quickly scaffold type definitions from example JSON data

Migration

Port JSON structures between languages with correct naming conventions

Documentation

Auto-generate type definitions for documentation or code review

Input Format

Paste a JSON object or array. The tool accepts:
  • Strict JSON
  • Relaxed JSON (unquoted keys, single quotes, trailing commas)
  • Nested objects and arrays
Example
{
  "user": {
    "id": 123,
    "name": "Alice",
    "email": "[email protected]",
    "is_active": true,
    "tags": ["admin", "verified"]
  }
}

Output Formats

Generates interfaces with TypeScript type annotations. Keys that aren’t valid identifiers (e.g., "first-name") are quoted.
interface User {
  id: number;
  name: string;
  email: string;
  is_active: boolean;
  tags: string[];
}

interface Root {
  user: User;
}

Type Inference Rules

  • nullnull (TS), Optional[Any] (Python), interface{} (Go), Option<serde_json::Value> (Rust)
  • true/falseboolean, bool, bool, bool
  • Numbers → number (TS), int/float (Python), int64/float64 (Go), i64/f64 (Rust)
  • Strings → string, str, string, String
  • Empty arrays → unknown[], List[Any], []interface{}, Vec<serde_json::Value>
  • Non-empty → Infers element type from first item: T[], List[T], []T, Vec<T>
  • Each object becomes a named interface/struct/class
  • Field names are sanitized:
    • TypeScript: Invalid identifiers are quoted
    • Python: Converted to snake_case, comments preserve original names
    • Go: Converted to PascalCase, json tags preserve original names
    • Rust: Converted to snake_case, #[serde(rename)] when needed

Implementation Details

The tool recursively traverses the JSON structure, collecting type definitions in a Map to avoid duplicates. Each target language has its own infer() function and naming conventions. Source: lib/tools/json-to-code.ts:5-143
Type inference is heuristic. For arrays, the tool infers the type from the first element only. Heterogeneous arrays will produce inaccurate types.

Keyboard Shortcuts

  • Cmd/Ctrl+Enter — Generate TypeScript (default)
  • Cmd/Ctrl+Shift+C — Copy output
  • Cmd/Ctrl+Shift+S — Download output

JSON Format/Validate

Validate and format JSON with prettify/minify

JSON to YAML

Convert JSON to YAML format

cURL to Code

Convert cURL commands to code

Build docs developers (and LLMs) love