JSON to Pydantic converter

Convert JSON to Pydantic v2 models from sample JSON: a model per nested object, dates as datetime, and fields that are missing in some items marked optional.

JSON

Pydantic

Waiting for input

What you get

How each result is checked

  1. Your JSON is validated by the generated models with the real pydantic 2 (CPython in WebAssembly, in your browser).
  2. Test values built from the inferred schema are validated by pydantic and by a reference validator (Ajv); they must agree, apart from the differences listed in the notes.

What can't convert exactly

  • Types come from one sample. A field that is null in the sample can only be typed None; add a sample with a real value for a better type.
  • The first run downloads Python and pydantic (about 6 MB compressed), once.

Questions

Can I give several examples?

Put them in an array: fields present in only some items become optional, and differing types become unions.

How do I turn JSON to Pydantic model classes?

Paste a sample of your JSON. You get a Pydantic v2 BaseModel for the document and for every nested object, and the real pydantic checks that your JSON passes them before you copy the code.

How do I make a Pydantic model from an API response?

Paste the JSON response here. You get a Pydantic model from the API response (and one for each nested object), then load data with Root.model_validate(response.json()). This is the quickest way from JSON to a Python class with type checking.

Do I need to install anything?

Nothing to install: no package, CLI or account. It runs in your browser, so you can try a schema before adding a generator to your build.

How do I convert JSON to Pydantic online?

Paste your JSON or open a file on the left (or click Example). The Pydantic appears on the right as you type, already checked. Copy it, or download it as a file. Nothing is uploaded.

Can the Pydantic output be wrong?

Every result is checked before it is shown (the checks are listed on this page). If a check fails, the page says "Review needed" and why, instead of presenting the output as correct, and anything that cannot convert exactly is named in the notes.