Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/json/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def main():
infile = open(options.infile, encoding='utf-8')
try:
if options.json_lines:
objs = (json.loads(line) for line in infile)
lines = infile.readlines()
objs = (json.loads(line) for line in lines)
else:
objs = (json.load(infile),)
finally:
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_json/json_lines.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"ingredients":["frog", "water", "chocolate", "glucose"]}
{"ingredients":["chocolate","steel bolts"]}
11 changes: 10 additions & 1 deletion Lib/test/test_json/test_tool.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import errno
import pathlib
import os
import sys
import textwrap
import unittest
import subprocess

from test import support
from test.support import os_helper
from test.support import force_not_colorized, os_helper
from test.support.script_helper import assert_python_ok


Expand Down Expand Up @@ -147,6 +148,14 @@ def test_jsonlines(self):
self.assertEqual(process.stdout, self.jsonlines_expect)
self.assertEqual(process.stderr, '')

@force_not_colorized
def test_jsonlines_from_file(self):
jsonl = pathlib.Path(__file__).parent / 'json_lines.jsonl'
args = sys.executable, '-m', 'json.tool', '--json-lines', jsonl
process = subprocess.run(args, 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')
self.assertEqual(rc, 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix "I/O operation on closed file" when parsing JSON Lines file with
:mod:`JSON CLI <json.tool>`.
Loading