This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add a --minify argument to json.tool
Type: enhancement Stage: resolved
Components: IO Versions: Python 3.9
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: bsolomon1124, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-08-29 19:17 by bsolomon1124, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15601 closed bsolomon1124, 2019-08-29 19:35
Messages (6)
msg350817 - (view) Author: Brad Solomon (bsolomon1124) * Date: 2019-08-29 19:17
I propose adding a command line `--minify` flag to the json/ module. 

This flag, if specified, uses `indent=None` and `separators=(',', ':')` to eliminate indent and separator whitespace in the output.

Minifying JSON (as is also done frequently with JS, CSS, and sometimes HTML) is common practice, and would be useful to have as a command-line tool rather than a user needing to use an online resource to do so.
msg350819 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-29 19:43

ISTM that minifying isn't something one usually does with a command-line tool.  Instead, it is part of the program generating JSON in the first place.  IOW, minification is useful in the standard library but not a part of json.tool which normally used a a pretty printer (perhaps to *undo* someone else's minification).
msg350821 - (view) Author: Brad Solomon (bsolomon1124) * Date: 2019-08-29 19:52
Since, as you point out, json.tool is made for convenience, I see the reverse of pretty-printing (minifying) being just as convenient:

$ cat > expanded.json <<EOF
> {
>     "foo": "bar",
>     "json": "obj"
> }
$ ./python.exe -m json.tool --minify expanded.json minf.json
$ cat minf.json 
msg350833 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-29 21:43
json.tool produces more readable representation. Your option is opposite to this purpose.
msg350835 - (view) Author: Brad Solomon (bsolomon1124) * Date: 2019-08-29 22:13
> json.tool produces more readable representation. Your option is opposite to this purpose.

That is correct, though I'm not sure what point you're trying to make.  The purpose of minifying isn't to make the input more readable; it's to condense it in the interest of sending fewer bytes over a network.  Unless I'm missing something, json.tool doesn't have the single express purpose of prettifying, since it also serves to be a validator as stated in its help message.  This is just aadding another commonly-sought option.
msg350837 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-29 22:24
Brad, thank you for the suggestion, but our goals for this tool are somewhat limited, so I'm going to close this as being out of scope for the intended purpose of json.tool.

If some other core developer wants to champion this, feel free to re-open.
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82163
2019-08-29 22:24:05rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg350837

stage: patch review -> resolved
2019-08-29 22:13:33bsolomon1124setmessages: + msg350835
2019-08-29 21:43:03serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg350833
2019-08-29 19:52:06bsolomon1124setmessages: + msg350821
2019-08-29 19:43:20rhettingersetnosy: + rhettinger
messages: + msg350819
2019-08-29 19:35:03bsolomon1124setkeywords: + patch
stage: patch review
pull_requests: + pull_request15277
2019-08-29 19:17:07bsolomon1124create