JSON to Pydantic
Generate Python Pydantic v2 models from JSON. Supports BaseModel, dataclasses, and TypedDict. Handles nested objects, lists, and Optional types securely.
Convert any JSON payload into production-ready Pydantic v2 models in one click. The generator
infers Python types correctly (distinguishing int from float), converts camelCase keys to snake_case, and builds separate class definitions for nested
objects — ideal for FastAPI, data validation, and schema enforcement.
How the Conversion Works
This generator maps JSON primitives to their exact Python equivalents and recursively processes complex data structures to build flat class hierarchies.
| JSON Type | Python Type | Notes |
|---|---|---|
| String | str | Standard text mapping |
| Number (Integer) | int | Numbers without decimals |
| Number (Decimal) | float | Numbers with decimal points |
| Boolean | bool | true/false becomes True/False |
| null | Optional[Any] | Defaults to None |
| Object | CustomClass | Spawns a new BaseModel class |
| Array | List[...] | Uses standard typing.List |
Handling Lists: When an array contains multiple objects, the generator merges all object shapes. This ensures that if one item has an extra key, the final model will include all possible fields from the array items.
Optional Fields: By default, null values are mapped as Any. If you check "Optional fields", it will wrap them in Optional[Any] = None.
Online Tool vs datamodel-code-generator
If you search for Pydantic code generation, you will likely find the excellent datamodel-code-generator CLI library. Here is when to use which:
Use this Online Tool when:
- You have a raw JSON payload (e.g. from an API request).
- You need models instantly without installing Python packages.
- You want to quickly draft types for a one-off FastAPI endpoint.
- You don't have a formal OpenAPI or JSON Schema specification.
Use the CLI tool when:
- You have a formal OpenAPI spec or JSON Schema file.
- You want to integrate model generation into your CI/CD pipeline.
- You need advanced features like field aliases, custom validators, or regex patterns.
Pydantic v1 vs v2 Output
The code generated by this tool is primarily designed for Pydantic v2, but due to the simplicity of the generated shapes, it is highly backwards-compatible.
Both v1 and v2 use the standard from pydantic import BaseModel import, and the type annotation syntax for classes remains identical. The generated code strictly uses type hints without complex Field() constraints, meaning you can safely paste the output into codebases using either version.
Looking for other languages? Try our JSON to TypeScript or JSON to Go generators.
Related tools
Frequently asked questions
What is Pydantic and why should I use it?
Pydantic is Python's most popular data validation library. It uses type annotations to validate, parse, and serialize data at runtime. It's the foundation of FastAPI's request and response models, and it's widely used for config management, data pipelines, and webhook validation. Pydantic v2 (released 2023) is 5–50× faster than v1 thanks to a Rust core.
Does this support Pydantic v2?
Yes! The generated BaseModel syntax is fully compatible with Pydantic v2. Because it generates standard Python type annotations without legacy Config classes, the models will work out of the box with the latest Pydantic versions.
Can I convert JSON Schema (not just JSON) to Pydantic?
This tool is designed to convert raw JSON data payloads (like an API response) into Pydantic models. It does not parse JSON Schema files. If you need to generate models from an OpenAPI spec or a JSON Schema, we recommend using the official datamodel-code-generator CLI.
What is the difference between BaseModel, dataclass, and TypedDict?
BaseModel is Pydantic's core class — it gives you validation, serialization, and a rich API. dataclass uses Python's standard decorator with Pydantic validation. TypedDict is a plain Python dict with type hints — no validation, but useful for static type checking.
How are JSON field names converted to Python?
JSON APIs commonly use camelCase (createdAt) while Python convention is snake_case (created_at). The generator converts automatically.
How does the tool handle nested objects and arrays?
Nested JSON objects become separate class definitions. Arrays of objects produce
List[ClassName] annotations where the class name is singularized from the key. If multiple objects
exist in the array, their shapes are merged so all possible fields are included.
Is my JSON sent to a server?
No, all code generation happens 100% locally in your browser. Your JSON data never leaves your computer, making it completely safe to use with sensitive API payloads.