JSON to Pydantic Model

Generate Pydantic v2 BaseModel classes from a JSON sample. Field types are inferred (str, int, float, bool, list, datetime, EmailStr). Nested objects become nested classes. Output is ready to drop into a Python file.

How to use the JSON to Pydantic Model

Paste a JSON sample. The generator infers Python types and writes a Pydantic class for the root object plus one for each nested object. Output uses Pydantic v2 syntax (the current major version). Imports needed are listed at the top of the output.

Generating Pydantic models from JSON

Pydantic is how modern Python validates data at its edges — FastAPI request bodies, settings, parsed API responses — but writing a BaseModel by hand from a sample payload is tedious, especially getting the type hints, nested models, and optional fields right. A sample of real JSON already contains everything needed to infer that model.

This reads a JSON sample and emits Pydantic v2 classes: scalar fields typed as str, int, float, or bool, nested objects promoted to their own BaseModel, and, when detection is on, richer types like datetime, EmailStr, and HttpUrl. You can name the root class and make every field Optional. For a TypeScript equivalent that validates at runtime, the JSON to Zod generator is the counterpart.

Common use cases

  • Model an API — turn a sample response into a typed Pydantic model.
  • FastAPI bodies — generate request and response models from example JSON.
  • Nested classes — get a separate BaseModel for each nested object.
  • Rich types — infer datetime, EmailStr, and HttpUrl where they apply.
  • Loosen fields — make every field Optional when the payload varies.

Frequently asked questions

Which Pydantic version does it target?

Pydantic v2, the current major version, with the imports needed listed at the top of the output.

How are nested objects handled?

Each nested object becomes its own BaseModel, referenced from the parent field.

What special types can it detect?

With detection on, it infers datetime, EmailStr, and HttpUrl in addition to the basic str, int, float, and bool.

Is there a TypeScript equivalent?

Yes — the JSON to Zod generator produces runtime-validating schemas for TypeScript.
Embed this tool on your site

Free to embed, no attribution required (but appreciated). Paste this where you want the tool to appear: