def flatten(d, parent_key='', sep='$'):
    """ Flattens a nested dict.
    Source: http://stackoverflow.com/questions/6027558/

    >>> d = {'id':1, 'pre':{'ogg':'x', 'mp3':'y'}}
    >>> sorted(flatten(d).items())
    [('id', 1), ('pre$mp3', 'y'), ('pre$ogg', 'x')]

    """
    items = []
    for k, v in d.items():
        new_key = parent_key + sep + k if parent_key else k
        if isinstance(v, dict):
            items.extend(flatten(v, new_key, sep=sep).items())
        else:
            items.append((new_key, v))
    return dict(items)