-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_pretty_print.py
executable file
·43 lines (32 loc) · 1.11 KB
/
json_pretty_print.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Code example that copies json from stdin and pretty prints json to terminal. Same as doing "cat <json file> | python -m json.tool"
Usage: cat <json file> | python json_pretty_print.py
"""
import argparse
import json
import sys
def help_text():
"""
Shows help text (top of file) when user does: python json_pretty_print.py -h
"""
text = argparse.ArgumentParser(
description='Code example that prints json content to terminal in pretty format. Same as doing "cat <json file> | python -m json.tool"',
usage='cat <json file> | python %(prog)s').parse_args()
return text
def main():
"""
Reads the a json file outputted to stdin, converts it to a dictionary, and then
prints to CLI in pretty json format.
:return: None
"""
help_text()
# grabbing lines from stdin
file_content = ""
for line in sys.stdin:
file_content += line
# converts the json into a dictionary
all_reactions = json.loads(file_content)
# prints to terminal
print(json.dumps(all_reactions, sort_keys=True, indent=4))
if __name__ == "__main__":
main()