<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_json(self):
 """Converts given document to JSON dict. """

        json_data = dict()

        for field_name, field_obj in self._fields.items():
            if isinstance(field_obj, NestedDocumentField):
                nested_document = field_obj.__get__(self, self.__class__)
                value = None if nested_document is None else nested_document.to_json()
            elif isinstance(field_obj, BaseField):
                value = field_obj.__get__(self, self.__class__)
                value = field_obj.to_json(value)
            else:
                # ignore fields not derived from BaseField or NestedDocument
                continue

            if value is None:
                # skip fields with None value 
                continue

            json_data[field_name] = value

        return json_data