Last active
August 24, 2024 14:46
-
-
Save KinoAR/a5cf8a207529ee643389c4462ebf13cd to your computer and use it in GitHub Desktop.
A JSON minify script written in Python.
This file contains 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
#!/usr/bin/env python3 | |
"""JSON minify program. """ | |
import json # import json library | |
import sys # import sys library | |
def minify(file_name): | |
"Minify JSON" | |
file_data = open(file_name, "r", 1).read() # store file info in variable | |
json_data = json.loads(file_data) # store in json structure | |
json_string = json.dumps(json_data, separators=(',', ":")) # Compact JSON structure | |
file_name = str(file_name).replace(".json", "") # remove .json from end of file_name string | |
new_file_name = "{0}_minify.json".format(file_name) | |
open(new_file_name, "w+", 1).write(json_string) # open and write json_string to file | |
ARGS = sys.argv[1:] # get arguments passed to command line excluding first arg | |
for arg in ARGS: # loop through arguments | |
minify(arg) |
10x
Good job! But in some cases, we prefer not to call json.loads
and minify the JSON string without deserializing it to an object. Do you have any suggestion for that?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks