Convert SQLAlchemy models to JSON!

Paste SQLAlchemy ORM models and get an example JSON! Handy for API tests and mock request bodies.
SQLAlchemy
Result
 

Convert SQLAlchemy models to JSON!

Paste SQLAlchemy model classes and get example JSON keyed by the column attribute names. It reads 2.0-style Mapped[...] with mapped_column(), 1.x-style Column(), and Flask-SQLAlchemy's db.Column(), and merges in columns from mixins and parent classes.

SQLAlchemy models cannot be passed to json.dumps directly, so FastAPI and Flask apps add Pydantic or Marshmallow schemas. Use this to lay out which fields a model will expose before writing that schema, or to build request bodies for tests.

How to use

  1. Paste the model file into the SQLAlchemy box. Keep Base, mixins and enum.Enum classes in the same input so inherited columns and enum values resolve.
  2. Only classes with __tablename__ count as models. Several models are output by class name, like { "User": {...}, "Team": {...} }.
  3. Copy the result.

Example

Input (Python · Flask-SQLAlchemy)
class TimestampMixin:
    created_at = db.Column(db.DateTime, default=datetime.utcnow)


class Plan(enum.Enum):
    FREE = "free"
    PRO = "pro"


class Subscription(TimestampMixin, db.Model):
    __tablename__ = "subscriptions"

    id = db.Column(db.Integer, primary_key=True)
    plan = db.Column(db.Enum(Plan), nullable=False)
    seats = db.Column(db.SmallInteger, default=1)
    trial_ends = db.Column(db.Date)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    user = db.relationship("User", backref="subscriptions")
Output (JSON)
{
  "created_at": "1970-01-01T00:00:00",
  "id": 0,
  "plan": "free",
  "seats": 1,
  "trial_ends": "1970-01-01",
  "user_id": 0
}

created_at from TimestampMixin comes first, following inheritance order, and user is dropped because relationship() is not a column. db.Enum(Plan) takes the enum's first value, "free", and db.Date becomes 1970-01-01. seats shows 1 from default=1, while created_at keeps the placeholder because datetime.utcnow is a function.

Supported features

  • Python type hints such as Mapped[int], Mapped[Optional[str]] and Mapped[str | None] decide the value.
  • Without a type hint, the SQL type argument decides, as in Column(String(50)) or db.Column(db.Numeric).
  • DateTime → 1970-01-01T00:00:00, Date → 1970-01-01, Time → 00:00:00, JSON/JSONB → {}, Uuid → an all-zero UUID.
  • ARRAY(String) and Mapped[list[str]] become one-element arrays.
  • A class extending enum.Enum, used via Enum(Plan) or Mapped[Plan], contributes its first member's value.
  • relationship(), column_property(), plain class variables and dunder attributes like __table_args__ are excluded.
  • Columns from mixins and parent models are placed before the child's own columns.

Use cases

Drafting a Pydantic response model

Before writing the Pydantic class for FastAPI's response_model, check the fields and types the model exposes as JSON. Feed that JSON to the JSON to Python page to get a Pydantic draft as well.

Test payloads for a Flask API

Build the body for client.post("/subscriptions", json=...) with keys that match the model's attributes. Strip the autoincrement id and creation time first.

Notes and limitations

  • A literal default= ("new", 1, True, None) is used as the sample value; callables like datetime.utcnow or dict are not run. server_default= and nullable= are not read.
  • If a column is given a separate database name, as in Column("user_name", String), the JSON key is still the Python attribute name.
  • Related objects loaded through relationship are not included. Add nested objects yourself if your response embeds them.
  • Computed attributes like hybrid_property or @property are not part of the output.

FAQ

Why is Mapped[Optional[str]] an empty string instead of null?

Optional means None is allowed; the example shows the case where a value is present, so it uses the inner str. Change it to null in the output if you need that case.

Why do datetimes have no Z or offset?

DateTime columns are naive by default, and isoformat() prints them without an offset. If you use DateTime(timezone=True), an example with +00:00 is closer to what you will actually see.

Can it convert Django models?

No. This page understands SQLAlchemy's Column, mapped_column and Mapped syntax only; Django fields like models.CharField are not recognized.