Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a191b91 commit 940e207Copy full SHA for 940e207
4 files changed
Doc/library/json.rst
@@ -104,6 +104,8 @@ Using json.tool from the shell to validate and pretty-print::
104
$ echo '{1.2:3.4}' | python -mjson.tool
105
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
106
107
+See :ref:`json-commandline` for detailed documentation.
108
+
109
.. highlight:: python3
110
111
.. note::
@@ -563,3 +565,52 @@ the last name-value pair for a given name::
563
565
{'x': 3}
564
566
567
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
@@ -10,21 +10,24 @@
10
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
11
12
"""
13
-import sys
+import argparse
14
import json
15
+import sys
16
17
18
def main():
- if len(sys.argv) == 1:
- infile = sys.stdin
19
- outfile = sys.stdout
20
- elif len(sys.argv) == 2:
21
- infile = open(sys.argv[1], 'r')
22
23
- elif len(sys.argv) == 3:
24
25
- outfile = open(sys.argv[2], 'w')
26
- else:
27
- raise SystemExit(sys.argv[0] + " [infile [outfile]]")
+ prog = 'python -m json.tool'
+ description = ('A simple command line interface for json module '
+ 'to validate and pretty-print JSON objects.')
+ parser = argparse.ArgumentParser(prog=prog, description=description)
+ parser.add_argument('infile', nargs='?', type=argparse.FileType(),
+ help='a JSON file to be validated or pretty-printed')
+ parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
+ help='write the output of infile to outfile')
+ options = parser.parse_args()
28
29
+ infile = options.infile or sys.stdin
30
+ outfile = options.outfile or sys.stdout
31
with infile:
32
try:
33
obj = json.load(infile)
Lib/test/test_json/test_tool.py
@@ -55,6 +55,7 @@ def _create_infile(self):
55
def test_infile_stdout(self):
56
infile = self._create_infile()
57
rc, out, err = assert_python_ok('-m', 'json.tool', infile)
58
+ self.assertEqual(rc, 0)
59
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
60
self.assertEqual(err, b'')
61
@@ -65,5 +66,12 @@ def test_infile_outfile(self):
65
66
self.addCleanup(os.remove, outfile)
67
with open(outfile, "r") as fp:
68
self.assertEqual(fp.read(), self.expect)
69
70
self.assertEqual(out, b'')
71
72
73
+ def test_help_flag(self):
74
+ rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
75
76
+ self.assertTrue(out.startswith(b'usage: '))
77
+ self.assertEqual(err, b'')
Misc/NEWS
@@ -23,6 +23,8 @@ Core and Builtins
Library
-------
+- Issue #21000: Improve the command-line interface of json.tool.
- Issue #20995: Enhance default ciphers used by the ssl module to enable
better security an prioritize perfect forward secrecy.
0 commit comments