import json


def to_bundle(sparkSession, dataset, resourceTypeUrl):
    """
    Converts a dataset of FHIR resources to a bundle containing those resources.
    Use with caution against large datasets.

    :param sparkSession: the SparkSession instance
    :param dataset: a DataFrame of encoded FHIR Resources
    :param resourceTypeUrl: the type of the FHIR resource to extract
        (Condition, Observation, etc, for the base profile, or the URL of the structure definition)
    :return: a JSON bundle of the dataset contents
    """

    jvm = sparkSession._jvm

    json_string = jvm.com.cerner.bunsen.stu3.python.Functions.toJsonBundle(dataset._jdf,
                                                                           resourceTypeUrl)

    return json.loads(json_string)