DevForge

JSON to Pydantic

Most JSON arrives in camelCase and most Python is written in snake_case, so a faithful model needs both: an idiomatic attribute name and an alias back to the original key. This generates that, along with the distinction Pydantic cares about between a field that may be null and a field that may be absent.

json
pydantic v2
options
target

A single object cannot reveal which keys are optional. Paste an array of several samples to detect them.

how to use it

  1. 01Paste a JSON object, an array of samples, or NDJSON.
  2. 02Check the inferred optional and nullable flags.
  3. 03Copy the model into your project.

questions

What is the difference between a null field and a missing one?
A field that can be null is written str | None. A field that may be absent also gets a default, as str | None = None, so Pydantic does not require it.
When is an alias added?
Whenever the Python name differs from the JSON key, including for keys that collide with a Python keyword. A model with any alias also gets populate_by_name so both spellings work.
Why is EmailStr optional?
It needs the separate email-validator package, so it is off by default and the generated code says so when you turn it on.

related