import json


def json2csv(jsondata, header=True): #---------------------------------------<<<
    """Convert JSON data to CSV.

    jsondata = string containing a JSON document
               e.g., open('filename.json').read()
    header = whether to output a CSV header row of field names

    Returns a string of the CSV version of the JSON data.
    """
    jsondoc = json.loads(jsondata)
    if not jsondoc:
        return '' # no JSON data found

    fldnames = sorted([field for field in jsondoc[0]])
    csvdata = ','.join(fldnames) + '\n' if header else ''

    for row in jsondoc:
        values = [row[fldname] for fldname in fldnames]
        csvdata += ','.join(values) + '\n'

    return csvdata