import json


def _parse_bjobs(bjobs_output_str):
    """Parse records from bjobs json type output.
    This snippet was taken from: https://github.com/DataBiosphere/toil/blob/eb2ae8365ae2ebdd50132570b20f7d480eb40cac/src/toil/batchSystems/lsf.py#L331
    :param bjobs_output_str: stdout of bjobs json type output
    :return: list with the jobs
    """
    bjobs_dict = None
    bjobs_records = None
    # Handle Cannot connect to LSF. Please wait ... type messages
    dict_start = bjobs_output_str.find("{")
    dict_end = bjobs_output_str.rfind("}")
    if dict_start != -1 and dict_end != -1:
        bjobs_output = bjobs_output_str[dict_start : (dict_end + 1)]
        bjobs_dict = json.loads(bjobs_output)
        return bjobs_dict["RECORDS"]
    if bjobs_records is None:
        raise ValueError(f"Could not find bjobs output json in: {bjobs_output_str}")
    return []