Saturday 18 September 2021

Pydantic: schema: Return dictionary of the json schema

BaseModel.schema return a dict of the json schema.

 

Example

emp1JsonSchema = Employee.schema()

Find the below working application.

 

schema_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()

print('emp1 -> ' + str(emp1) + '\n\n')
print('emp1JsonSchema -> ' + str(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']}


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment