import six


def _unicode_to_str(data):
    """ Utility function to make json encoded data an ascii string
        Original taken from:
        http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python

    :param data: either a dict, list or unicode string
    :return: ascii data
    """
    if isinstance(data, dict):
        return {_unicode_to_str(key): _unicode_to_str(value) for key, value in six.iteritems(data)}
    elif isinstance(data, list):
        return [_unicode_to_str(element) for element in data]
    elif isinstance(data, six.text_type):
        return data.encode('utf-8')
    else:
        return data