Convert JSON to SQLAlchemy Schema!

Input JSON to generate SQLAlchemy 2.0 ORM models (Mapped, mapped_column)! For Flask and FastAPI projects.
JSON
Result
 

Convert JSON to SQLAlchemy Schema!

Converts JSON into a ready-to-run SQLAlchemy 2.0 declarative model: a Base(DeclarativeBase) class, Mapped[...] type hints with mapped_column(), nullable flags, __tablename__ and an Integer primary key.

It is a good starting point when a FastAPI or Flask backend has to store request JSON, or data downloaded from another system, in a table that has no model yet.

How to use

  1. Paste a JSON object representing one table row. If some rows have empty values, paste several rows as an array so nullable=True is picked up.
  2. Enter the class name in Model name (default MyModel); __tablename__ is that name in upper snake case.
  3. Save the output as models.py. If your project already has a Base, delete the generated one and import yours, so all models share one metadata.

Example

Input (JSON)
{
  "isbn": "978-0-13-468599-1",
  "title": "The Pragmatic Programmer",
  "pages": 352,
  "rating": 4.7,
  "available": true,
  "published_at": "2019-09-13"
}
Output (SQLAlchemy 2.0)
from datetime import date

from sqlalchemy import Boolean, Date, Float, Integer, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


class Base(DeclarativeBase):
    pass


class Book(Base):
    __tablename__ = "BOOK"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    isbn: Mapped[str] = mapped_column(String(255), nullable=False)
    title: Mapped[str] = mapped_column(String(255), nullable=False)
    pages: Mapped[int] = mapped_column(Integer, nullable=False)
    rating: Mapped[float] = mapped_column(Float, nullable=False)
    available: Mapped[bool] = mapped_column(Boolean, nullable=False)
    published_at: Mapped[date] = mapped_column(Date, nullable=False)

The hyphenated isbn stays String(255), pages becomes Integer, rating Float, and published_at, a date without a time, Date with a date hint. Every value is present, so all columns are nullable=False.

Supported features

  • Defines class Base(DeclarativeBase): pass and imports only the SQLAlchemy types and datetime names actually used.
  • Type mapping: integers → Integer/int (BigInteger for large values), decimals → Float, booleans → Boolean, YYYY-MM-DD → Date/date, date-times → DateTime/datetime, objects and arrays → JSON with dict/list.
  • Strings are String(255), or Text when a sample value is longer than 255 characters, so the table can be created on MySQL too.
  • Attributes are snake_case. When the JSON key differs (publishedAt, home-url), the original key is passed as the column name: mapped_column("publishedAt", ...). Python keywords and metadata get a trailing _.
  • Nullable columns are hinted Mapped[str | None] as well as nullable=True.
  • The primary key is Integer, which autoincrements on SQLite as well; a string id in your JSON becomes a String(255) key, and a large integer id uses BigInteger().with_variant(Integer, "sqlite").

Use cases

Persistence models for FastAPI endpoints

If you already have the sample request JSON for a Pydantic schema, generate the SQLAlchemy model from the same JSON and write the session.add(Book(**payload)) flow right away.

Autogenerated Alembic migrations

Once the model is registered on Base.metadata, alembic revision --autogenerate produces a create_table migration from this class.

Notes and limitations

  • str | None hints need Python 3.10+. On older versions, write Optional[str] with from typing import Optional.
  • __tablename__ is upper case (BOOK). PostgreSQL then needs it quoted in raw SQL, so many projects change it to books.
  • With Flask-SQLAlchemy, inherit from db.Model instead of the generated Base.
  • Float is binary floating point; use Numeric(10, 2) with Decimal for prices.

FAQ

Why does id use Integer rather than BigInteger?

SQLite only autoincrements a column declared exactly as INTEGER PRIMARY KEY, so Integer keeps local tests and production behaving the same. Only when your data already has ids beyond 32 bits does the key switch to BigInteger with an SQLite variant.

Is there a schema (__table_args__) setting?

No. The table goes into the connection's default schema. To target another PostgreSQL schema, add __table_args__ = {"schema": "billing"} yourself.

Can I get SQLAlchemy 1.4 Column() syntax?

Output is 2.0 style only (Mapped, mapped_column, DeclarativeBase). For 1.4, use declarative_base() and rewrite each line as title = Column(String(255), nullable=False).