| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -717,6 +717,12 @@ Command line options | |||
| 717 | 717 | ||
| 718 | 718 | .. versionadded:: 3.5 | |
| 719 | 719 | ||
| 720 | + .. cmdoption:: --json-lines | ||
| 721 | + | ||
| 722 | + Parse every input line as separate JSON object. | ||
| 723 | + | ||
| 724 | + .. versionadded:: 3.8 | ||
| 725 | + | ||
| 720 | 726 | .. cmdoption:: -h, --help | |
| 721 | 727 | ||
| 722 | 728 | Show the help message. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -151,6 +151,12 @@ by right-clicking the button. (Contributed by Tal Einat in :issue:`1529353`.) | |||
| 151 | 151 | The changes above have been backported to 3.7 maintenance releases. | |
| 152 | 152 | ||
| 153 | 153 | ||
| 154 | + json.tool | ||
| 155 | + --------- | ||
| 156 | + | ||
| 157 | + Add option ``--json-lines`` to parse every input line as separate JSON object. | ||
| 158 | + (Contributed by Weipeng Hong in :issue:`31553`.) | ||
| 159 | + | ||
| 154 | 160 | os.path | |
| 155 | 161 | ------- | |
| 156 | 162 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,19 +26,25 @@ def main(): | |||
| 26 | 26 | help='write the output of infile to outfile') | |
| 27 | 27 | parser.add_argument('--sort-keys', action='store_true', default=False, | |
| 28 | 28 | help='sort the output of dictionaries alphabetically by key') | |
| 29 | + parser.add_argument('--json-lines', action='store_true', default=False, | ||
| 30 | + help='parse input using the jsonlines format') | ||
| 29 | 31 | options = parser.parse_args() | |
| 30 | 32 | ||
| 31 | 33 | infile = options.infile or sys.stdin | |
| 32 | 34 | outfile = options.outfile or sys.stdout | |
| 33 | 35 | sort_keys = options.sort_keys | |
| 34 | - with infile: | ||
| 36 | + json_lines = options.json_lines | ||
| 37 | + with infile, outfile: | ||
| 35 | 38 | try: | |
| 36 | - obj = json.load(infile) | ||
| 39 | + if json_lines: | ||
| 40 | + objs = (json.loads(line) for line in infile) | ||
| 41 | + else: | ||
| 42 | + objs = (json.load(infile), ) | ||
| 43 | + for obj in objs: | ||
| 44 | + json.dump(obj, outfile, sort_keys=sort_keys, indent=4) | ||
| 45 | + outfile.write('\n') | ||
| 37 | 46 | except ValueError as e: | |
| 38 | 47 | raise SystemExit(e) | |
| 39 | - with outfile: | ||
| 40 | - json.dump(obj, outfile, sort_keys=sort_keys, indent=4) | ||
| 41 | - outfile.write('\n') | ||
| 42 | 48 | ||
| 43 | 49 | ||
| 44 | 50 | if __name__ == '__main__': | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,6 +60,28 @@ class TestTool(unittest.TestCase): | |||
| 60 | 60 | ] | |
| 61 | 61 | """) | |
| 62 | 62 | ||
| 63 | + jsonlines_raw = textwrap.dedent("""\ | ||
| 64 | + {"ingredients":["frog", "water", "chocolate", "glucose"]} | ||
| 65 | + {"ingredients":["chocolate","steel bolts"]} | ||
| 66 | + """) | ||
| 67 | + | ||
| 68 | + jsonlines_expect = textwrap.dedent("""\ | ||
| 69 | + { | ||
| 70 | + "ingredients": [ | ||
| 71 | + "frog", | ||
| 72 | + "water", | ||
| 73 | + "chocolate", | ||
| 74 | + "glucose" | ||
| 75 | + ] | ||
| 76 | + } | ||
| 77 | + { | ||
| 78 | + "ingredients": [ | ||
| 79 | + "chocolate", | ||
| 80 | + "steel bolts" | ||
| 81 | + ] | ||
| 82 | + } | ||
| 83 | + """) | ||
| 84 | + | ||
| 63 | 85 | def test_stdin_stdout(self): | |
| 64 | 86 | args = sys.executable, '-m', 'json.tool' | |
| 65 | 87 | with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: | |
@@ -92,6 +114,13 @@ def test_infile_outfile(self): | |||
| 92 | 114 | self.assertEqual(out, b'') | |
| 93 | 115 | self.assertEqual(err, b'') | |
| 94 | 116 | ||
| 117 | + def test_jsonlines(self): | ||
| 118 | + args = sys.executable, '-m', 'json.tool', '--json-lines' | ||
| 119 | + with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: | ||
| 120 | + out, err = proc.communicate(self.jsonlines_raw.encode()) | ||
| 121 | + self.assertEqual(out.splitlines(), self.jsonlines_expect.encode().splitlines()) | ||
| 122 | + self.assertEqual(err, b'') | ||
| 123 | + | ||
| 95 | 124 | def test_help_flag(self): | |
| 96 | 125 | rc, out, err = assert_python_ok('-m', 'json.tool', '-h') | |
| 97 | 126 | self.assertEqual(rc, 0) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Add the --json-lines option to json.tool. Patch by hongweipeng. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments