35 lines
988 B
Python
35 lines
988 B
Python
#!/usr/bin/env python3
|
|
"""JSON minifier: removes all unnecessary whitespace from JSON files.
|
|
|
|
Usage: json-minify.py <input.json> [output.json]
|
|
If output not specified, writes to stdout.
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def minify_json(input_path: str, output_path: str | None = None) -> None:
|
|
"""Read JSON, minify it, and write result."""
|
|
with open(input_path, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
|
|
minified = json.dumps(data, separators=(',', ':'), ensure_ascii=False)
|
|
|
|
if output_path:
|
|
Path(output_path).write_text(minified + '\n', encoding='utf-8')
|
|
print(f"Minified {input_path} -> {output_path}", file=sys.stderr)
|
|
else:
|
|
print(minified)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 2:
|
|
print(__doc__, file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
input_file = sys.argv[1]
|
|
output_file = sys.argv[2] if len(sys.argv) > 2 else None
|
|
|
|
minify_json(input_file, output_file) |