import json


def dict2json(dict_, pretty=True):
    """Convert dict to json (string).

    :param dict_: dictionary to be converted
    :type dict_: dict
    :param pretty: if True, nice formatting will be used
    :type pretty: bool
    :return: formatted dict in json
    :rtype: str
    """
    kwargs = {}
    if pretty:
        kwargs['sort_keys'] = True
        kwargs['separators'] = (',', ': ')
        kwargs['indent'] = 2

    return json.dumps(dict_, **kwargs)