Logo JSON Parser Online

Generate Python Pydantic Models from JSON

Paste JSON and click Generate Python. The tool inspects every field and nested object, infers Python types, and produces Pydantic BaseModel class definitions you can drop into a Python project. Useful as a starting point for API response models, config schemas, and data validation.

All JSON Tools

28 tools

Free, fast, runs in your browser. No login needed.

Made with ❤️ in India · © 2025 parsejsononline.com
INPUT
1
OUTPUT
1
✅ Ready
Characters: 0
Ctrl+Enter to run
What is this tool?

How to use

  1. Paste your JSON into the INPUT pane.
  2. Click Generate Python.
  3. Copy the output or download it as a .py file.
  4. Add Optional[...] wrappers and default values for fields that may be absent in production data.

How it works

The generator walks your JSON recursively, inferring a Pydantic type for each value:

  • Strings → str
  • Integers → int (detected via Number.isInteger())
  • Floats → float
  • Booleans → bool
  • Null → Optional[Any]
  • Arrays → List[T] where T is inferred from the first element; empty arrays → List[Any]
  • Nested objects → a new BaseModel subclass, named by concatenating the parent class name and the capitalized key name (e.g., key address in RootModel → RootModelAddress)

All fields are required. No field is marked Optional unless its JSON value is null. If some fields can be absent in your actual data, wrap their type with Optional[T] = None manually.

Array type inference uses only the first element. If your array contains mixed types, the inferred List[T] will only reflect the first element's type. Adjust to List[Union[...]] or List[Any] manually.

Compatible with Pydantic v1 and v2. The generated classes use BaseModel with plain field annotations — no v1-only decorators, no v2-specific model_config. They work in both versions.

FAQ

Is the output for Pydantic v1 or v2?

Both. The generated classes use plain BaseModel field annotations which are compatible with both Pydantic v1 and v2. No version-specific features are used.

How are nested objects handled?

Each nested object gets its own BaseModel subclass. The class name is built by concatenating the parent class name and the capitalized key name — for example, "address" inside "RootModel" becomes "RootModelAddress". Classes are emitted in dependency order so inner classes appear before the classes that reference them.

Why are all fields required?

The generator infers types from a single JSON sample. It cannot know which fields are optional in production. Add Optional[T] = None manually for fields that may be absent.

What happens with arrays of mixed types?

Only the first element is used to infer the List type. If your array holds mixed types, change List[InferredType] to List[Any] or List[Union[...]] manually.

Is my data sent anywhere?

No. The generation runs entirely in your browser.

Related tools

JSON to TypeScriptGenerate TypeScript interfaces from the same JSON. JSON SchemaGenerate a JSON Schema draft-07 definition instead. JSON ValidatorValidate JSON syntax before generating models.