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.

Technical Guide & Mechanics

Generating Robust Pydantic v2 Models from JSON Data

Pydantic v2 is the foundation for modern Python data validation, powering FastAPI, LangChain, and high-performance backend microservices. However, real-world JSON payloads typically use camelCase keys, whereas idiomatic Python requires snake_case field names.

Our generator automatically converts field names to snake_case while generating Pydantic Field(alias=...) definitions and setting model_config = ConfigDict(populate_by_name=True). This ensures seamless two-way serialization.

The model generator strictly differentiates between optional fields (keys that may be omitted) and nullable fields (values that may be None), generating modern Python 3.10+ union types (str | None = None).

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.
Is Pydantic v2 syntax used?
Yes, the generated models use ConfigDict and modern Python type unions, fully compliant with Pydantic v2.

related