import json


def sheet_to_json(sheet):
    """ Converts elements of an Excel worksheet into a json string.

    A worksheet that looks like this:
    | endovar | description           | units | <-- header row
    | p       | aggregate price index | index |
    Gets converted to this:
    "p":{"units": "index", "description": "aggregate price index"}

    Args: 
        sheet: A worksheet object from the xlrd module.

    Returns:
        A json string with data from the Excel worksheet.
    """
    
    headers = sheet.row_values(0, 1, sheet.ncols)
    
    jsons = ['{']
    for r in range(1, sheet.nrows):
        val = sheet.cell_value(r, 0)
        if isinstance(val, float): val = "{:.0f}".format(val)
        jsons.append('"' + val + '":')
        data = sheet.row_values(r, 1, sheet.ncols);
        jsons.append(json.dumps(dict(zip(headers, data))) + ',')

    return "".join(jsons)[:-1] + '}'