BaseModel.schema_json() method return the json schema of this model in string format.
Example
emp1JsonSchema = Employee.schema_json(indent=2)
Argument ‘indent’ is used to pretty print the json string.
schema_json_1.py
from pydantic import BaseModel
class Employee(BaseModel):
    id: int
    name: str
    age: int
emp1 = Employee(id = 1, name = 'Krishna', age = 23)
emp1JsonSchema = Employee.schema_json(indent=2)
print('emp1 -> ' + str(emp1) + '\n\n')
print('emp1JsonSchema -> ' + emp1JsonSchema)
Output
emp1 -> id=1 name='Krishna' age=23
emp1JsonSchema -> {
  "title": "Employee",
  "type": "object",
  "properties": {
    "id": {
      "title": "Id",
      "type": "integer"
    },
    "name": {
      "title": "Name",
      "type": "string"
    },
    "age": {
      "title": "Age",
      "type": "integer"
    }
  },
  "required": [
    "id",
    "name",
    "age"
  ]
}
 
No comments:
Post a Comment