from io import StringIO
import csv
import json


def convert_csv_content_to_json(csv_content):
    """
    Convert CSV content to a JSON string

    return json_string, None
    return None, err_msg
    """
    if not csv_content:
        return (None, 'No content specified')

    # Read the CSV content into python dicts
    #
    string_io = StringIO(csv_content)

    rows = None
    with string_io as f:
        reader = csv.DictReader(f)
        rows = list(reader)

    # Failed to read csv into rows
    #
    if not rows:
        return (None, 'Failed to read content as a csv')

    # Format the rows into a JSON string
    #
    try:
        json_content = json.dumps(rows)
    except TypeError:
        return (None, 'Failed to convert csv content to JSON')

    return (json_content, None)