Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 940e207

Browse files
committed
improve the command-line interface of json.tool (closes #21000)
A patch from Berker Peksag.
1 parent a191b91 commit 940e207

4 files changed

Lines changed: 76 additions & 12 deletions

File tree

Doc/library/json.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Using json.tool from the shell to validate and pretty-print::
104104
$ echo '{1.2:3.4}' | python -mjson.tool
105105
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
106106

107+
See :ref:`json-commandline` for detailed documentation.
108+
107109
.. highlight:: python3
108110

109111
.. note::
@@ -563,3 +565,52 @@ the last name-value pair for a given name::
563565
{'x': 3}
564566

565567
The *object_pairs_hook* parameter can be used to alter this behavior.
568+
569+
.. highlight:: bash
570+
571+
.. _json-commandline:
572+
573+
Command Line Interface
574+
----------------------
575+
576+
The :mod:`json.tool` module provides a simple command line interface to validate
577+
and pretty-print JSON objects.
578+
579+
If the optional :option:`infile` and :option:`outfile` arguments are not
580+
specified, :attr:`sys.stdin` and :attr:`sys.stdout` will be used respectively::
581+
582+
$ echo '{"json": "obj"}' | python -m json.tool
583+
{
584+
"json": "obj"
585+
}
586+
$ echo '{1.2:3.4}' | python -m json.tool
587+
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
588+
589+
590+
Command line options
591+
^^^^^^^^^^^^^^^^^^^^
592+
593+
.. cmdoption:: [<infile>]
594+
595+
The JSON file to be validated or pretty-printed::
596+
597+
$ python -m json.tool mp_films.json
598+
[
599+
{
600+
"title": "And Now for Something Completely Different",
601+
"year": 1971
602+
},
603+
{
604+
"title": "Monty Python and the Holy Grail",
605+
"year": 1975
606+
}
607+
]
608+
609+
.. cmdoption:: [<outfile>]
610+
611+
Write the output of the *infile* to the given *outfile*. Otherwise, write it
612+
to :attr:`sys.stdout`.
613+
614+
.. cmdoption:: -h, --help
615+
616+
Show the help message.

Lib/json/tool.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@
1010
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
1111
1212
"""
13-
import sys
13+
import argparse
1414
import json
15+
import sys
16+
1517

1618
def main():
17-
if len(sys.argv) == 1:
18-
infile = sys.stdin
19-
outfile = sys.stdout
20-
elif len(sys.argv) == 2:
21-
infile = open(sys.argv[1], 'r')
22-
outfile = sys.stdout
23-
elif len(sys.argv) == 3:
24-
infile = open(sys.argv[1], 'r')
25-
outfile = open(sys.argv[2], 'w')
26-
else:
27-
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
19+
prog = 'python -m json.tool'
20+
description = ('A simple command line interface for json module '
21+
'to validate and pretty-print JSON objects.')
22+
parser = argparse.ArgumentParser(prog=prog, description=description)
23+
parser.add_argument('infile', nargs='?', type=argparse.FileType(),
24+
help='a JSON file to be validated or pretty-printed')
25+
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
26+
help='write the output of infile to outfile')
27+
options = parser.parse_args()
28+
29+
infile = options.infile or sys.stdin
30+
outfile = options.outfile or sys.stdout
2831
with infile:
2932
try:
3033
obj = json.load(infile)

Lib/test/test_json/test_tool.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def _create_infile(self):
5555
def test_infile_stdout(self):
5656
infile = self._create_infile()
5757
rc, out, err = assert_python_ok('-m', 'json.tool', infile)
58+
self.assertEqual(rc, 0)
5859
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
5960
self.assertEqual(err, b'')
6061

@@ -65,5 +66,12 @@ def test_infile_outfile(self):
6566
self.addCleanup(os.remove, outfile)
6667
with open(outfile, "r") as fp:
6768
self.assertEqual(fp.read(), self.expect)
69+
self.assertEqual(rc, 0)
6870
self.assertEqual(out, b'')
6971
self.assertEqual(err, b'')
72+
73+
def test_help_flag(self):
74+
rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
75+
self.assertEqual(rc, 0)
76+
self.assertTrue(out.startswith(b'usage: '))
77+
self.assertEqual(err, b'')

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Core and Builtins
2323
Library
2424
-------
2525

26+
- Issue #21000: Improve the command-line interface of json.tool.
27+
2628
- Issue #20995: Enhance default ciphers used by the ssl module to enable
2729
better security an prioritize perfect forward secrecy.
2830

0 commit comments

Comments
 (0)