Created
July 14, 2025 18:07
-
-
Save PierceLBrooks/6a3cdc8de3f37fb9569a46521f90dbbe to your computer and use it in GitHub Desktop.
jsonprettify.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Pierce Brooks | |
import os | |
import sys | |
import json | |
import shutil | |
import logging | |
import traceback | |
import subprocess | |
for root, folders, files in os.walk(os.getcwd()): | |
for name in files: | |
if not (name.endswith(".json")): | |
continue | |
path = os.path.join(root, name) | |
descriptor = open(path, "r") | |
content = descriptor.read() | |
descriptor.close() | |
command = [] | |
command.append(sys.executable) | |
command.append("-m") | |
command.append("json.tool") | |
command.append(path) | |
try: | |
output = subprocess.check_output(command) | |
while (os.path.exists(path)): | |
path += ".json" | |
descriptor = open(path, "wb") | |
descriptor.write(output) | |
descriptor.close() | |
os.remove(os.path.join(root, name)) | |
shutil.copy(path, os.path.join(root, name)) | |
os.remove(path) | |
except: | |
if (sys.flags.debug): | |
logging.error(traceback.format_exc()) | |
path = os.path.join(root, name) | |
if not (os.path.exists(path)): | |
descriptor = open(path, "w") | |
descriptor.write(content) | |
descriptor.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment