N
NJHTools

JSON to Python Dataclass & Pydantic Model

Paste JSON and get typed Python classes for every nested object, as standard-library dataclasses or Pydantic BaseModel models. Free, no signup, and generated in your browser so nothing is uploaded.

Language

16 lines · processed in your browser, nothing is uploaded

Python output · root.py

Generated code appears here.

Example: JSON to Python

This JSON sample, with the root type named User, produces the Python below using the default options.

Input JSON
{
  "id": 1024,
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "active": true,
  "score": 98.5,
  "tags": [
    "admin",
    "beta"
  ],
  "address": {
    "city": "London",
    "postcode": "NW1 6XE"
  }
}
Generated Python
from dataclasses import dataclass
from typing import List


@dataclass
class Address:
    city: str
    postcode: str


@dataclass
class User:
    id: int
    name: str
    email: str
    active: bool
    score: float
    tags: List[str]
    address: Address

How to convert JSON to Python

  1. 1Paste a JSON object or array into the input.
  2. 2Select Python as the output language.
  3. 3Choose the model style: dataclasses or Pydantic BaseModel.
  4. 4Pick your target Python version: 3.6, 3.7+ or 3.10+.
  5. 5Name the root class, for example Product.
  6. 6Copy the code or download it as a .py file.

Dataclasses or Pydantic?

Standard-library dataclasses give you typed containers with no dependencies, but they do not validate or coerce values; a string where an int is expected passes silently. They suit internal data you already trust.

Pydantic models validate and convert data on construction and raise clear errors for bad input. They suit API responses, config files and request bodies, and frameworks such as FastAPI use them directly. Pick the style with the model style option.

Using the generated Pydantic models

Install pydantic, then parse a dict with Product.model_validate(data) in Pydantic v2 (parse_obj in v1), or a raw string with Product.model_validate_json(text). Invalid input raises a ValidationError that lists every failing field.

If a JSON key is not a valid Python identifier or you want a different attribute name, map it with Field(alias="original-key").

Python version and optional fields

The version setting controls which language features and typing syntax the output may use. Set it to the oldest Python version you deploy to; standard-library dataclasses, for example, only exist from Python 3.7.

A field that can be missing or null is typed as optional and should default to None. Paste several records so the generator can see which keys are sometimes absent or null.

Frequently Asked Questions

How do I convert JSON to a Pydantic model?↓

Paste the JSON, choose Python, set the model style to Pydantic BaseModel and copy or download the code. Each nested object becomes its own model class.

Should I use dataclasses or Pydantic for JSON?↓

Use dataclasses for trusted internal data with zero dependencies. Use Pydantic when data comes from outside your program and needs validation and type conversion.

How are optional fields detected?↓

Types are inferred from the sample you paste. A field becomes optional only if it is missing from some of the objects, so paste an array with several representative records, or mark the field optional by hand after generating.

Which Python versions are supported?↓

You can target Python 3.6, 3.7+ or 3.10+. Choose the oldest version you run in production so the generated code uses only features that version supports.

Does it handle nested lists?↓

Yes. Arrays of objects become typed lists of a generated class, and nested arrays become nested list types.

Is my JSON uploaded to a server?↓

No. The code generator (quicktype) runs entirely in your browser, so the JSON you paste never leaves your device. That makes it safe to use with internal API responses, though you should still strip secrets before sharing generated code.

Last updated: