def echo_json_response(response, pretty, limit=None):
    '''Wrapper to echo JSON with optional 'pretty' printing. If pretty is not
    provided explicity and stdout is a terminal (and not redirected or piped),
    the default will be to indent and sort keys'''
    indent = None
    sort_keys = False
    if pretty or (pretty is None and sys.stdout.isatty()):
        indent = 2
        sort_keys = True
    try:
        if hasattr(response, 'json_encode'):
            response.json_encode(click.get_text_stream('stdout'), limit=limit,
                                 indent=indent, sort_keys=sort_keys)
        else:
            res = response.get_raw()
            res = json.dumps(json.loads(res), indent=2, sort_keys=True)
            click.echo(res)
    except __HOLE__ as ioe:
        # hide scary looking broken pipe stack traces
        raise click.ClickException(str(ioe))
dataset/ETHPy150Open planetlabs/planet-client-python/planet/scripts/__init__.py/echo_json_response