[New-bugs-announce] [issue29636] Specifying indent in the json.tool command

Daniel Himmelstein report at bugs.python.org
Thu Feb 23 15:05:56 EST 2017


New submission from Daniel Himmelstein:

The utility of `python -m json.tool` would increase if users could specify the indent level.

Example use case: newlines in a JSON document are important for readability and the ability to open in a text editor. However, if the file is large, you can save space by decreasing the indent level.

I added an --indent argument to json.tool in https://github.com/python/cpython/pull/201. However, design discussion is required since indent can take an int, string, or None. In addition, a indent string is a tab, which is difficult to pass via a command line argument.

Currently, I added the following function to convert the indent option to the indent parameter of json.dump:

```
def parse_indent(indent):
    """Parse the argparse indent argument."""
    if indent == 'None':
        return None
    if indent == r'\t':
        return '\t'
    try:
        return int(indent)
    except ValueError:
        return indent
```

@inada.naoki mentioned the special casing is undesirable. I agree, but can't think of an alternative. Advice appreciated.

----------
components: IO
messages: 288479
nosy: dhimmel, inada.naoki
priority: normal
pull_requests: 230
severity: normal
status: open
title: Specifying indent in the json.tool command
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29636>
_______________________________________


More information about the New-bugs-announce mailing list