code: def pre_process_json(obj):
      
      if type(obj) is dict:
          new_dict = {}
          for key, value in obj.items():
              new_dict[key] = pre_process_json(value)
          return new_dict
  
      elif type(obj) is list:
          new_list = []
          for item in obj:
              new_list.append(pre_process_json(item))
          return new_list
  
      elif hasattr(obj, 'todict'):
          return dict(obj.todict())
  
      else:
          try:
              json.dumps(obj)
          except TypeError:
              try:
                  json.dumps(obj.__dict__)
              except TypeError:
                  return str(obj)
              else:
                  return obj.__dict__
          else:
              return obj
docs: Preprocess items in a dictionary or list and prepare them to be json serialized.