import json


def kv_format(event):
    """
    Format record to be stored in V3IO KV table. Removes apostrophes in text fields,
    flattens lists into strings, and adds the title as the key for the KV record.
    
    Expects "title", "summarized_text", and optional "original_text" fields
    in event.
    """
    # Remove commas from title and text
    event.body["title"] = event.body["title"].replace("'", "")
    event.body["summarized_text"] = event.body["summarized_text"].replace("'", "")
    if "original_text" in event.body:
        event.body["original_text"] = event.body["original_text"].replace("'", "")
    
    # Add KV key
    event.key = event.body["title"]
    
    # Flatten list into string
    for k, v in event.body.items():
        if type(v) == list:
            event.body[k] = json.dumps(v)
    
    return event