Skip to content

Instantly share code, notes, and snippets.

@PythonCoderAS
Last active October 17, 2019 00:34
Show Gist options
  • Select an option

  • Save PythonCoderAS/f1577a54d5316cc622e0f0fd0dc7659a to your computer and use it in GitHub Desktop.

Select an option

Save PythonCoderAS/f1577a54d5316cc622e0f0fd0dc7659a to your computer and use it in GitHub Desktop.
Easy way to minify json files with python

The shell script will find all .json files that do not have .min in the filename, and then pass the file contents to the python program.

The python program will accept the json through stdin.

import sys
import re
json = sys.stdin.read()
try:
j = eval(json)
except SyntaxError:
sys.stderr.write("The inputted JSON is invalid, please pipe in valid JSON")
sys.exit(1)
else:
sys.stdout.write(re.sub(r'\s+(?=([^"]*"[^"]*")*[^"]*$)', "", str(j).replace("'", '"')))
find . -maxdepth 1 -name "*.json" ! -name ".min" -exec cat {} \; | python minify.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment