from typing import Union


def convert_timestamps_to_str(json_element: Union[dict, list]) -> Union[dict, list]:
    """Converts timestamps in JSON object/array from Python Datetime to str

    This methods recursively iterates through a given JSON object or array and tries to convert every element
    into a string of the format %Y-%m-%dT%H:%M:%S.%fZ
    """
    if isinstance(json_element, dict):
        for k, v in json_element.items():
            try:
                json_element[k] = v.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
            except AttributeError:
                convert_timestamps_to_str(json_element[k])
    elif isinstance(json_element, list):
        for item in json_element:
            convert_timestamps_to_str(item)
    return json_element