title: Fastapi and Pydantic to build POST API: TypeError: Object of type is not JSON serializable
body: <p>I have a problem with FastAPI and Pydantic.</p>
  <p>I want to build a post api, program show this:</p>
  <pre class="lang-py prettyprint-override"><code>@router.post('/productRoute', response_model=SuccessCreate, status_code=status.HTTP_201_CREATED)
  async def create_product_route(create: CreatePR):
      query = ProductRouteModel.insert().values(
          user_id=create.user_id,
          route_id=create.route_id,
          route_name=create.route_name,
          head=create.head.dict(),
          body=create.body,
          route=create.route
      )
      await database.execute(query)
      return {&quot;status&quot;: &quot;Successfully Created!&quot;}
  </code></pre>
  <p>This is Pydantic class:</p>
  <pre class="lang-py prettyprint-override"><code>class RouteSchema(BaseModel):
      id: str
      next: Optional[List[str]]
      prev: List[str]
  
  
  class HeadSchema(BaseModel):
      b1: str
      b2: str
      b3: str
  
  
  class BodySchema(BaseModel):
      a1: Optional[str]
      a2: Optional[str]
  
  
  class CreatePR(BaseModel):
      user_id: str
      route_id: str
      route_name: str
      head: HeadSchema
      body: List[BodySchema]
      route: List[RouteSchema]
  </code></pre>
  <p>Finally, this i want to post's parameter format:</p>
  <pre class="lang-json prettyprint-override"><code>{
      user_id: &quot;test1&quot;,
      route_id: &quot;route_1&quot;,
      route_name: &quot;route_name&quot;,
      head: {...},
      body: [{...}, {...}, ..., {...}],
      route: [{...}, {...}, ..., {...}]
  }
  </code></pre>
  <p>When I executed, I got TypeError: Object of type BodySchema is not JSON serializable.</p>
  <p>How can I fix the program to normal operation?</p>