import json


def filterPOSTToJSON(body, fields):
    """
    Extract a post body by field name into a sane format for JSON dump
    """
    filtered = {}
    for k in fields:
        fk = k
        if k.endswith('[]'):
            fk = k[:-2]
            filtered[fk] = body.getlist(k)
        else:
            filtered[fk] = body[k]
    return json.dumps(filtered)