Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

105 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CI main workflow badge

JSON to Python Types

json2pyi infers a type schema from a sample JSON file and generates Python type definitions (dataclass, Pydantic BaseModel or PEP-589 TypedDict) accordingly. It runs in browser, requiring no installation.

🌐 Available online 🔗: https://json2pyi.pages.dev

Example

Input:

{
    "page": {
        "id": "kctbh9vrtdwd",
        "name": "GitHub",
        "url": "https://www.githubstatus.com",
        "time_zone": "Etc/UTC",
        "updated_at": "2020-12-03T08:11:21.385Z"
    },
    "components": [
        {
            "id": "8l4ygp009s5s",
            "name": "Git Operations",
            "status": "operational",
            "created_at": "2017-01-31T20:05:05.370Z",
            "updated_at": "2020-10-29T22:51:43.831Z",
            "position": 1,
            "description": "Performance of git clones, pulls, pushes, and associated operations",
            "showcase": false,
            "start_date": null,
            "group_id": null,
            "page_id": "kctbh9vrtdwd",
            "group": false,
            "only_show_if_degraded": false
        },
        /* ... */
    ],
    "incidents": [],
    "scheduled_maintenances": [],
    "status": {
        "indicator": "none",
        "description": "All Systems Operational"
    }
}

Output:

@dataclass
class Page:
    id = str
    name = str
    url = str
    time_zone = str
    updated_at = str

@dataclass
class Components:
    id = str
    name = str
    status = str
    created_at = str
    updated_at = str
    position = int
    description = Optional[str]
    showcase = bool
    start_date = None
    group_id = None
    page_id = str
    group = bool
    only_show_if_degraded = bool

@dataclass
class Status:
    indicator = str
    description = str

@dataclass
class RootObject:
    page = Page
    components = list[Components]
    incidents = list[Any]
    scheduled_maintenances = list[Any]
    status = Status

Or:

from typing import TypedDict, Optional

from datetime import datetime


IncidentUpdate = TypedDict("IncidentUpdate", {"body": str, "created_at": datetime, "display_at": datetime, "id": str, "incident_id": str, "status": str, "updated_at": datetime})

IncidentOrScheduledMaintenance = TypedDict("IncidentOrScheduledMaintenance", {"created_at": datetime, "id": str, "impact": str, "incident_updates": list[IncidentUpdate], "monitoring_at": None, "name": str, "page_id": str, "resolved_at": None, "shortlink": str, "status": str, "updated_at": datetime, "scheduled_for": Optional[datetime], "scheduled_until": Optional[datetime]})

Component = TypedDict("Component", {"created_at": datetime, "description": None, "id": str, "name": str, "page_id": str, "position": int, "status": str, "updated_at": datetime})

Status = TypedDict("Status", {"description": str, "indicator": str})

Page = TypedDict("Page", {"id": str, "name": str, "url": str, "updated_at": datetime})

RootObject = TypedDict("UnnammedType3C2BC8", {"page": Page, "status": Status, "components": list[Component], "incidents": list[IncidentOrScheduledMaintenance], "scheduled_maintenances": list[IncidentOrScheduledMaintenance]})

TODO

  • Detect tuple (array)
  • Detect UUID / datetime
  • Detect Enum
  • Merge data types with similar structure and common name prefix/suffix
  • Detect recursive type definition (e.g. tree)
  • Include imports of non-primitive types
  • Generate type alias for complex Union
  • Improve the logic of determining whether a Union ix complex or not
  • Generate TypedDict
  • Refactor to unify TypedDict and dataclass generation Seperated intendedly for clear code structure.
  • Compile to WASM and provide a Web-based app
  • Allow to tweak more options on Web app (partially blocked by vhiribarren/raytracer-rust#8)
  • Avoid merging data types with totally different structures in a union
  • Avoid unnecessary heap allocation by reducing one-time usage of Vec
  • Allow specifying the order of generated data types
  • Support more input types, such as JSON Schema
  • Support more target languages
  • Add usage instructions

Credits

The project is inspired by:

About

Generate Python type definitions from a JSON sample (both Pydantic BaseModel and TypedDict are supported)

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages