['[2.0', ', 1.0', ']']
-Using :mod:`json.tool` from the shell to validate and pretty-print:
+Using :mod:`json` from the shell to validate and pretty-print:
.. code-block:: shell-session
- $ echo '{"json":"obj"}' | python -m json.tool
+ $ echo '{"json":"obj"}' | python -m json
{
"json": "obj"
}
- $ echo '{1.2:3.4}' | python -m json.tool
+ $ echo '{1.2:3.4}' | python -m json
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
See :ref:`json-commandline` for detailed documentation.
.. _json-commandline:
-.. program:: json.tool
+.. program:: json
-Command Line Interface
+Command-line interface
----------------------
.. module:: json.tool
- :synopsis: A command line to validate and pretty-print JSON.
+ :synopsis: A command-line interface to validate and pretty-print JSON.
**Source code:** :source:`Lib/json/tool.py`
--------------
-The :mod:`json.tool` module provides a simple command line interface to validate
-and pretty-print JSON objects.
+The :mod:`json` module can be invoked as a script via ``python -m json``
+to validate and pretty-print JSON objects. The :mod:`json.tool` submodule
+implements this interface.
If the optional ``infile`` and ``outfile`` arguments are not
specified, :data:`sys.stdin` and :data:`sys.stdout` will be used respectively:
.. code-block:: shell-session
- $ echo '{"json": "obj"}' | python -m json.tool
+ $ echo '{"json": "obj"}' | python -m json
{
"json": "obj"
}
- $ echo '{1.2:3.4}' | python -m json.tool
+ $ echo '{1.2:3.4}' | python -m json
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
.. versionchanged:: 3.5
:option:`--sort-keys` option to sort the output of dictionaries
alphabetically by key.
+.. versionchanged:: 3.14
+ The :mod:`json` module may now be directly executed as
+ ``python -m json``. For backwards compatibility, invoking
+ the CLI as ``python -m json.tool`` remains supported.
-Command line options
+
+Command-line options
^^^^^^^^^^^^^^^^^^^^
.. option:: infile
.. code-block:: shell-session
- $ python -m json.tool mp_films.json
+ $ python -m json mp_films.json
[
{
"title": "And Now for Something Completely Different",
@support.requires_subprocess()
-class TestTool(unittest.TestCase):
+class TestMain(unittest.TestCase):
data = """
[["blorpie"],[ "whoops" ] , [
"i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
:"yes"} ]
"""
+ module = 'json'
expect_without_sort_keys = textwrap.dedent("""\
[
""")
def test_stdin_stdout(self):
- args = sys.executable, '-m', 'json.tool'
+ args = sys.executable, '-m', self.module
process = subprocess.run(args, input=self.data, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, self.expect)
self.assertEqual(process.stderr, '')
def test_infile_stdout(self):
infile = self._create_infile()
- rc, out, err = assert_python_ok('-m', 'json.tool', infile)
+ rc, out, err = assert_python_ok('-m', self.module, infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
self.assertEqual(err, b'')
''').encode()
infile = self._create_infile(data)
- rc, out, err = assert_python_ok('-m', 'json.tool', infile)
+ rc, out, err = assert_python_ok('-m', self.module, infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(), expect.splitlines())
def test_infile_outfile(self):
infile = self._create_infile()
outfile = os_helper.TESTFN + '.out'
- rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile)
+ rc, out, err = assert_python_ok('-m', self.module, infile, outfile)
self.addCleanup(os.remove, outfile)
with open(outfile, "r", encoding="utf-8") as fp:
self.assertEqual(fp.read(), self.expect)
def test_writing_in_place(self):
infile = self._create_infile()
- rc, out, err = assert_python_ok('-m', 'json.tool', infile, infile)
+ rc, out, err = assert_python_ok('-m', self.module, infile, infile)
with open(infile, "r", encoding="utf-8") as fp:
self.assertEqual(fp.read(), self.expect)
self.assertEqual(rc, 0)
self.assertEqual(err, b'')
def test_jsonlines(self):
- args = sys.executable, '-m', 'json.tool', '--json-lines'
+ args = sys.executable, '-m', self.module, '--json-lines'
process = subprocess.run(args, input=self.jsonlines_raw, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, self.jsonlines_expect)
self.assertEqual(process.stderr, '')
def test_help_flag(self):
- rc, out, err = assert_python_ok('-m', 'json.tool', '-h')
+ rc, out, err = assert_python_ok('-m', self.module, '-h')
self.assertEqual(rc, 0)
self.assertTrue(out.startswith(b'usage: '))
self.assertEqual(err, b'')
def test_sort_keys_flag(self):
infile = self._create_infile()
- rc, out, err = assert_python_ok('-m', 'json.tool', '--sort-keys', infile)
+ rc, out, err = assert_python_ok('-m', self.module, '--sort-keys', infile)
self.assertEqual(rc, 0)
self.assertEqual(out.splitlines(),
self.expect_without_sort_keys.encode().splitlines())
2
]
''')
- args = sys.executable, '-m', 'json.tool', '--indent', '2'
+ args = sys.executable, '-m', self.module, '--indent', '2'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')
def test_no_indent(self):
input_ = '[1,\n2]'
expect = '[1, 2]\n'
- args = sys.executable, '-m', 'json.tool', '--no-indent'
+ args = sys.executable, '-m', self.module, '--no-indent'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')
def test_tab(self):
input_ = '[1, 2]'
expect = '[\n\t1,\n\t2\n]\n'
- args = sys.executable, '-m', 'json.tool', '--tab'
+ args = sys.executable, '-m', self.module, '--tab'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')
def test_compact(self):
input_ = '[ 1 ,\n 2]'
expect = '[1,2]\n'
- args = sys.executable, '-m', 'json.tool', '--compact'
+ args = sys.executable, '-m', self.module, '--compact'
process = subprocess.run(args, input=input_, capture_output=True, text=True, check=True)
self.assertEqual(process.stdout, expect)
self.assertEqual(process.stderr, '')
infile = self._create_infile('{"key":"💩"}')
outfile = os_helper.TESTFN + '.out'
self.addCleanup(os.remove, outfile)
- assert_python_ok('-m', 'json.tool', '--no-ensure-ascii', infile, outfile)
+ assert_python_ok('-m', self.module, '--no-ensure-ascii', infile, outfile)
with open(outfile, "rb") as f:
lines = f.read().splitlines()
# asserting utf-8 encoded output file
infile = self._create_infile('{"key":"💩"}')
outfile = os_helper.TESTFN + '.out'
self.addCleanup(os.remove, outfile)
- assert_python_ok('-m', 'json.tool', infile, outfile)
+ assert_python_ok('-m', self.module, infile, outfile)
with open(outfile, "rb") as f:
lines = f.read().splitlines()
# asserting an ascii encoded output file
@unittest.skipIf(sys.platform =="win32", "The test is failed with ValueError on Windows")
def test_broken_pipe_error(self):
- cmd = [sys.executable, '-m', 'json.tool']
+ cmd = [sys.executable, '-m', self.module]
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
- # bpo-39828: Closing before json.tool attempts to write into stdout.
+ # bpo-39828: Closing before json attempts to write into stdout.
proc.stdout.close()
proc.communicate(b'"{}"')
self.assertEqual(proc.returncode, errno.EPIPE)
+
+
+@support.requires_subprocess()
+class TestTool(TestMain):
+ module = 'json.tool'