-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathclean_json_file.py
executable file
·63 lines (52 loc) · 1.56 KB
/
clean_json_file.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
"""
Clean JSON files.
Clean one or more JSON files so as to fix indentation, order fields, and remove
trailing whitespace.
"""
import json
import sys
import click
@click.command()
@click.argument(
"filenames", metavar="[FILENAME] ...", type=click.Path(exists=True), nargs=-1
)
@click.option(
"--check",
is_flag=True,
default=False,
help="Don't write the file back, only check whether the file is well formatted.",
)
def clean_json_file(filenames, check): # noqa: D301
"""
Clean JSON files.
Clean one or more JSON files so as to fix indentation, order fields, and
remove trailing whitespace.
"""
problematic_files = []
for filename in list(set(filenames)):
with open(filename, "r") as fdesc:
old_content = fdesc.read()
records = json.loads(old_content)
new_content = (
json.dumps(
records,
indent=2,
sort_keys=True,
ensure_ascii=False,
separators=(",", ": "),
)
+ "\n"
)
if old_content != new_content:
if check:
problematic_files.append(filename)
else:
with open(filename, "w") as fdesc:
fdesc.write(new_content)
if check and problematic_files:
for filename in problematic_files:
print(f"[ERROR] File {filename} is badly formatted.")
sys.exit(1)
if __name__ == "__main__":
clean_json_file()