import json


def dump_into_json(filename, metrics):
    """Dump the metrics dictionary into a JSON file

    It will automatically dump the dictionary:
    metrics = {'duration': duration,
               'voltage_extremes': voltage_extremes,
               'num_beats': num_beats,
               'mean_hr_bpm': mean_hr_bpm,
               'beats': beats}.
    in to a JSON file with the file name as the data file name.

    :param filename: name of the file being read
    :param metrics: a dictionary containing duration,
                    voltage extremes, number of beats, beats per minute,
                    and the time where beats occur
    :returns:
        - successful_JSON - test if it has successfully create JSON
    """
    successful_JSON = False
    try:
        output_file = open(filename + '.json', 'w')
        json.dump(metrics, output_file)
        output_file.close()
        successful_JSON = True
    except TypeError:
        print("Unsuccessfully output JSON file")
    return successful_JSON