Skip to content

Instantly share code, notes, and snippets.

@jcarley
Created December 17, 2020 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcarley/1e6acf8efd2bc43b0eec9bfcb8fcb7d8 to your computer and use it in GitHub Desktop.
Save jcarley/1e6acf8efd2bc43b0eec9bfcb8fcb7d8 to your computer and use it in GitHub Desktop.
Minifies and escapes a json file. I use this for minifying json key files from GCP
#!/usr/bin/env bash
usage() {
echo "./jsonmin <INFILE> <OUTFILE>"
echo " INFILE: File to be minified"
echo " OUTIFLE: File to save minified json as"
}
INFILE=$1
OUTFILE=$2
if [[ -z "$INFILE" ]]; then
echo "Error: Missing parameter. Expect 2."
usage
exit 1
fi
if [[ -z "$OUTFILE" ]]; then
echo "Error: Missing parameter. Expect 2."
usage
exit 1
fi
if ! [ -x "$(command -v python)" ]; then
echo 'Error: python is not installed.' >&2
exit 1
fi
if ! [ -x "$(command -v jq)" ]; then
echo 'Error: jq is not installed.' >&2
exit 1
fi
if [[ $INFILE == $OUTFILE ]]; then
echo 'Error: The INFILE and OUTFILE can not be the same file.'
exit 1
fi
python -c 'import json, sys;json.dump(json.load(sys.stdin), sys.stdout)' < $INFILE | jq -aR . > $OUTFILE
echo "Successfully minified ${INFILE} to ${OUTFILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment