Skip to main content

Convert JSON Schema to TypeScript interfaces (wraps json-schema-to-typescript)

Project description

jsonschema-ts

Python to TypeScript converter, powered by json-schema-to-typescript

Convert JSON Schema (from Pydantic v2 or any source) into clean TypeScript interfaces. A thin Python wrapper around the battle-tested json-schema-to-typescript engine.

from jsonschema_ts import convert

schema = {
    "type": "object",
    "properties": {
        "id": {"type": "integer"},
        "name": {"type": "string"},
        "email": {"type": "string", "format": "email"},
    },
    "required": ["id", "name", "email"],
}

ts = convert(schema, "User")
# export interface User {
#   id: number;
#   name: string;
#   email: string;
# }

Features

  • Zero Python dependencies — pure Python stdlib
  • Battle-tested engine — wraps json-schema-to-typescript
  • Full JSON Schema support$ref, $defs, allOf, anyOf, oneOf, enums, tuples, circular refs, additional properties, const, and more
  • Pydantic v2 compatible — works directly with model_json_schema() output
  • Batch conversion — single npx call for all $defs
  • Clean output — Prettier-formatted, export interface declarations

Requirements

  • Python ≥ 3.11
  • Node.js ≥ 18 (with npx) — for the TypeScript generation engine

Installation

pip install jsonschema-ts

The first call to convert() will prompt npx to cache json-schema-to-typescript.

Usage

from jsonschema_ts import convert, convert_all, collect_defs, ensure_inline_models

# Single schema
ts = convert(schema, "MyType")

# With $defs (for Pydantic models)
defs = collect_defs(schema)
model_ts = convert_all(defs)

# Inline models from pydantic dataclasses (@model)
schemas = ensure_inline_models(schema1, schema2)
defs = collect_defs(*schemas)
model_ts = convert_all(defs)

# Full Pydantic pipeline
from pydantic import BaseModel

class Address(BaseModel):
    street: str
    city: str
    zip: str

class User(BaseModel):
    name: str
    address: Address

schema = User.model_json_schema()
defs = collect_defs(schema)
models = convert_all(defs)
main = convert(schema, "User")

API

| Function | Description | |---|---|---| | convert(schema, name) | Convert single JSON Schema → TypeScript interface | | convert_all(defs) | Convert all $defs → TypeScript interfaces (batch) | | collect_defs(*schemas) | Extract and merge $defs from schemas | | ensure_inline_models(*schemas) | Promote inline object schemas to $defs entries | | assemble(models, procedures) | Combine model interfaces + procedure types into final TS output | | ensure_npx() | Check Node.js/npx availability |

from jsonschema_ts import collect_defs, convert, convert_all, ensure_inline_models, assemble

# ... promote inline models, then collect and convert ...
schemas = ensure_inline_models(raw_schema)
defs = collect_defs(*schemas)
models = convert_all(defs)

# Combine everything into final output
output = assemble(
    models=models,
    procedures="export interface Types { greet(name: string): Promise<string>; }",
)

Why a wrapper?

json-schema-to-typescript has an 8-stage pipeline (resolver → linker → normalizer → parser → optimizer → generator → formatter) with 18 normalization rules handling hundreds of edge cases. Reimplementing this in Python would take months. This package delegates the heavy lifting and focuses on what Python does best: orchestration, $defs collection, and output assembly.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonschema_ts-0.1.2.tar.gz (83.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonschema_ts-0.1.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file jsonschema_ts-0.1.2.tar.gz.

File metadata

  • Download URL: jsonschema_ts-0.1.2.tar.gz
  • Upload date:
  • Size: 83.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for jsonschema_ts-0.1.2.tar.gz
Algorithm Hash digest
SHA256 fba9a55e6a8b39869e7c7656c1f648803527ddfbaabd830c7f5a9c37f3577667
MD5 fd5b0b6fe4412c9c8c93437e91eb0669
BLAKE2b-256 30dbeabeade4acb3f74c917c616a30bf065d6e7c2622c9b7d033c1eed3a16c55

See more details on using hashes here.

File details

Details for the file jsonschema_ts-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: jsonschema_ts-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for jsonschema_ts-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 62b6273931dca3fd59cf50043a20c195d4e15d15fcf1c5f0eadc5f8342281cde
MD5 a5b1a258a09d991c3d02e9f5076706b0
BLAKE2b-256 0ad2f8cf8c78230e1728403631b11cb318a887041ab759e802bcce12099847cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page