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
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
Output Formats
- TypeScript
- Python
- Go
- Rust
Generates interfaces with TypeScript type annotations. Keys that aren’t valid identifiers (e.g.,
"first-name") are quoted.Type Inference Rules
Primitives
Primitives
null→null(TS),Optional[Any](Python),interface{}(Go),Option<serde_json::Value>(Rust)true/false→boolean,bool,bool,bool- Numbers →
number(TS),int/float(Python),int64/float64(Go),i64/f64(Rust) - Strings →
string,str,string,String
Arrays
Arrays
- Empty arrays →
unknown[],List[Any],[]interface{},Vec<serde_json::Value> - Non-empty → Infers element type from first item:
T[],List[T],[]T,Vec<T>
Objects
Objects
- 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 aMap to avoid duplicates. Each target language has its own infer() function and naming conventions.
Source: lib/tools/json-to-code.ts:5-143
Keyboard Shortcuts
Cmd/Ctrl+Enter— Generate TypeScript (default)Cmd/Ctrl+Shift+C— Copy outputCmd/Ctrl+Shift+S— Download output
Related Tools
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