Created
December 17, 2020 19:25
-
-
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
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 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