from datetime import datetime


def parse_heart_json(input_json):
    """
    Input JSON from Shortcuts currently contains two keys:
    - hrDates: Contains an array of all timestamps (ascending order)
    - hrValues: Contains an array of all heart rates (sorted by timestamp, ascending)

    This converts the dictionary into a list of tuples: each containing a timestamp
    and the corresponding HR reading. If parsing fails, None is returned.
    """

    try:
        hr_dates = [datetime.strptime(x, '%Y-%m-%d %H:%M:%S') for x in input_json['hrDates']]
        hr_values = [float(x) for x in input_json['hrValues']]
        all_data = list(zip(hr_dates, hr_values)) 

        if(len(all_data) == 0):
            print("No health samples found in data. Nothing will be exported.")

        return(all_data)
    except ValueError as ve:
        print("Error parsing the dates or values returned from the Shortcuts app: {}".format(ve))
        return(None)