Skip to content

Instantly share code, notes, and snippets.

@clarkgrubb
Created May 9, 2018 01:35
Show Gist options
  • Save clarkgrubb/07ef4879a250185830ffdd42608e1acf to your computer and use it in GitHub Desktop.
Save clarkgrubb/07ef4879a250185830ffdd42608e1acf to your computer and use it in GitHub Desktop.
tool for comparing two json documents
#!/usr/bin/env bash
set -eu -o pipefail
if [ "$#" -lt 2 ]
then
echo "USAGE: json-diff [DIFF_OPTIONS] PATH1 PATH2" 1>&2
exit 2
fi
args=("$@")
file1=${args[$(( $# - 2 ))]}
file2=${args[$(( $# - 1 ))]}
unset args[$(( $# - 1 ))]
unset args[$(( $# - 2 ))]
normalized1=$(mktemp)
normalized2=$(mktemp)
function cleanup {
rm -f "$normalized1" "$normalized2"
}
trap cleanup ERR
function cleanup_and_exit {
cleanup
exit "$1"
}
if ! python3 -mjson.tool --sort-keys < "$file1" > "$normalized1"
then
cleanup_and_exit 2
fi
if ! python3 -mjson.tool --sort-keys < "$file2" > "$normalized2"
then
cleanup_and_exit 2
fi
set +u
diff "${args[@]}" "$normalized1" "$normalized2"
diff_retval=$?
set -u
cleanup_and_exit "$diff_retval"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment