nomalize_json_files.sh 988 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env bash
  2. # This script sorts the JSON files to make it easy to diff
  3. # which also makes the generated units to be easy to diff. :-)
  4. #
  5. # ** NOTICE **
  6. # This script requires jq to be installed. jq is
  7. # included in most distros package repos.
  8. # https://github.com/stedolan/jq/
  9. shopt -s nocaseglob #ignore case for filename matches
  10. shopt -s nullglob #if no matches return a null string
  11. SCRIPT_DIR="$(dirname "$0")"
  12. JSON_DIR="$SCRIPT_DIR/_google_api_json_tmp"
  13. TIMESTAMP=$(date +%F@%H%M) #format: 2006-09-15@1228
  14. LOG_FILE="$JSON_DIR/${TIMESTAMP}-$(basename "$0").txt" #log file with same name as script
  15. {
  16. TMP_FILE=$(mktemp --tmpdir="$JSON_DIR")
  17. ((COUNT=0))
  18. for FILE in "$JSON_DIR/"*.json; do
  19. echo "# running jq --sortkeys on: $FILE"
  20. jq --sort-keys '.' "$FILE" > "$TMP_FILE"
  21. mv -f "$TMP_FILE" "$FILE"
  22. ((COUNT++))
  23. done
  24. echo ""
  25. echo "Processed File Count = $COUNT"
  26. } |& tee "$LOG_FILE" #output both stdout and stderr to logfile and terminal