<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def insert_extracted_value(self, doc, extracted_value, output_field, original_output_field=None):
 """inserts the extracted value into doc at the field specified by output_field"""

        if not extracted_value:
            return doc
        metadata = self.extractor.get_metadata()
        if not self.extractor.get_include_context():
            if isinstance(extracted_value, list):
                result = list()
                for ev in extracted_value:
                    result.append({'value': ev})
            else:
                result = {'value': extracted_value}
        else:
            result = extracted_value
        metadata['result'] = result
        metadata['source'] = str(self.input_fields)

        if original_output_field is not None:
            metadata['original_output_field'] = original_output_field

        if self.name is not None:
            metadata['name'] = self.name

        field_elements = output_field.split('.')
        while len(field_elements) > 1:
            field_element = field_elements.pop(0)
            if '[' in field_element:
                if not field_element.startswith('['):
                    array_field_elements = field_element.split('[', 1)
                    array_field_element = array_field_elements[0]
                    doc = doc[array_field_element]
                    field_element = array_field_elements[1]
                array_elements = field_element.split(']')
                for array_element in array_elements:
                    if not array_element:
                        continue
                    if array_element.startswith('['):
                        array_element = array_element[1:]
                    if array_element.isdigit() and isinstance(doc, list):
                        doc = doc[int(array_element)]
                    else:
                        doc = doc[array_element]
            else:
                if field_element not in doc:
                    doc[field_element] = {}
                doc = doc[field_element]
        field_element = field_elements[0]
        if field_element in doc:
            output = doc[field_element]
            if isinstance(output, dict):
                output = [output, metadata]
            elif isinstance(output, types.ListType):
                output.append(metadata)

        else:
            output = [metadata]
        doc[field_element] = output