Files
datamodel-code-generator/docs/jsonschema.md
Noddy 2db3745127 Update documentation (#1316)
* Make small grammatical and phrasing changes, update external like to JSON schema docs, remove unused schema keywords, unify JSON Schema language

* Title case some headers, modify some phrasing, remove duplicate custom template docs, clean up using_as_module with phrasing and removal of redundant ending.

* Add crosslinks between documents for clarity, rephrase sectinos, unify representation of "JSON Schema", remove specifictation of temporary directory use

* Reword "File" to "Data" to include python dicts, update working on project mentions

* Update language from "repositories" to "projects" to include links that are not repos

* Small phrasing update

* Update phrasing of supported data page for clairty

* Unify representation of "JSON", remove redundant information from index

* Synchronize changes between readme and index page

* Standardize formatting, fix heading levels
2023-05-13 11:38:46 +09:00

1.5 KiB

Generate from JSON Schema

The code generator can create pydantic models from JSON Schema. See more information about supported JSON Schema data types and features here.

Example

$ datamodel-codegen  --input person.json --input-file-type jsonschema --output model.py

person.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    },
    "friends": {
      "type": "array"
    },
    "comment": {
      "type": "null"
    }
  }
}

model.py

# generated by datamodel-codegen:
#   filename:  person.json
#   timestamp: 2020-04-27T16:12:27+00:00

from __future__ import annotations

from typing import Any, List, Optional

from pydantic import BaseModel, Field, conint


class Person(BaseModel):
    firstName: Optional[str] = Field(None, description="The person's first name.")
    lastName: Optional[str] = Field(None, description="The person's last name.")
    age: Optional[conint(ge=0)] = Field(
        None, description='Age in years which must be equal to or greater than zero.'
    )
    friends: Optional[List] = None
    comment: Optional[Any] = None