def json_ld_convert_unit_to_reference_unit(db):
    """Convert the units to their reference unit. Also changes the format to eliminate unnecessary complexity.

    Changes:

        {
            'flow': {'refUnit': 'MJ', ...},
            'unit': {
                '@type': 'Unit',
                '@id': '86ad2244-1f0e-4912-af53-7865283103e4',
                'name': 'kWh'
        }

    To:

        {
            'flow': {...},
            'unit': 'MJ'
        }


    """
    unit_conversion = {
        unit["@id"]: unit["conversionFactor"]
        for group in db["unit_groups"].values()
        for unit in group["units"]
    }

    for ds in db["processes"].values():
        for exc in ds["exchanges"]:
            unit_obj = exc.pop("unit")
            exc["amount"] *= unit_conversion[unit_obj["@id"]]
            if "refUnit" in exc["flow"]:
                exc["unit"] = exc["flow"].pop("refUnit")
            else:
                exc["unit"] = unit_obj["name"]
    return db