DevKits

JSON to Python Dataclass Converter

Convert JSON to Python @dataclass definitions with typed fields. snake_case field names, nested classes, List[T] for arrays, and JSON-key alias comments. 100% local.

Last updated:

135 chars · 7 lines

Paste JSON to generate Python @dataclass definitions with typed fields (snake_case), nested classes, List[T] for arrays, and JSON-key alias comments. Ready for stdlib json.loads() with dacite, or easy to convert to pydantic. 100% local.

Tips & Best Practices

  • For strict runtime validation, swap @dataclass for pydantic.BaseModel — the field types transfer directly.
  • Provide a comprehensive sample: null values become Any, so any field that's null in the sample won't be tightly typed.
  • For deep JSON, the tool generates one dataclass per nested object with PascalCase names derived from the field name.

Frequently Asked Questions

Why dataclass and not TypedDict or Pydantic?

Dataclasses are stdlib (no third-party dependency) and cover the 95% case for API model shapes. The output is easy to adapt: swap @dataclass for pydantic.BaseModel, or paste into a TypedDict, and it keeps working.

How are field names converted?

JSON keys are converted to Python snake_case (e.g. userName → user_name). A trailing comment shows the original JSON key so tools like dacite or pydantic aliases can round-trip.

How are nested objects handled?

Each nested object becomes its own @dataclass named after the field (PascalCased). Arrays become List[T] where T is inferred from the first element.

Is my JSON uploaded anywhere?

No. All conversion runs locally in your browser as client-side JavaScript.

Related Tools