Skip to content

Instantly share code, notes, and snippets.

@bmiro
Last active June 22, 2021 08:57
Show Gist options
  • Save bmiro/0b785f9d556bd1a305762d4523522b95 to your computer and use it in GitHub Desktop.
Save bmiro/0b785f9d556bd1a305762d4523522b95 to your computer and use it in GitHub Desktop.
JSON diff tool for cli (jsondiff) using `jq` with able to show it on vim with `--vim`
#!/bin/bash
DIFF_PARAMS="-aur --color=auto"
# Precondition check
which jq > /dev/null || (echo "jq is required" && exit 1)
# Arg check
for arg in "$@"; do
shift
case "$arg" in
"--vim")
vim="true"
;;
*)
set -- "$@" "$arg"
esac
done
json1="$1"
json2="$2"
stat "$json1" > /dev/null || exit $?
stat "$json2" > /dev/null || exit $?
# Main
sorted1=$(mktemp --suffix=.jsondiff)
sorted2=$(mktemp --suffix=.jsondiff)
jq -S . "$json1" > "$sorted1"
jq -S . "$json2" > "$sorted2"
[ "$vim" ] && cmd=vimdiff || cmd="diff $DIFF_PARAMS"
$cmd "$sorted1" "$sorted2"
# Tear down
rm "$sorted1"
rm "$sorted2"
@bmiro
Copy link
Author

bmiro commented Mar 28, 2018

Install

Preconditions

apt install jq

JSONdiff

wget https://gist.githubusercontent.com/bmiro/0b785f9d556bd1a305762d4523522b95/raw/a26a54378ec82b0ad614fe7d02d6da056ec411c1/jsondiff
chmod a+x jsondiff
sudo mv jsondiff /usr/local/bin

Usage

Simple usage

jsondiff 1.json 2.json

Display on vimdiff (require apt install vim)

jsondiff --vim 1.json 2.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment