pjson
and pdiffjson
are simple bash scripts that are useful (and non-obvious) enough more
developers should have them at their fingertips.
They combine jq
, diff
, colordiff
, and less
to normalize, display, and diff JSON. Once you have them
handy, you may find yourself using them a lot.pjson
normalizes, colorizes, and displays one file or stdin.pdiffjson
normalizes and displays a colorizied diff of any two files.jq
’s speed.diff
./
), thanks to less
.npm
.Ensure you have jq
and colordiff
on your system.
Use brew install jq colordiff
on Mac, apt-get install jq colordiff
or equivalent on Linux.
Install the script anywhere you like. Simplest:
$ npm install -g pdiffjson
Or copy pjson
and pdiffjson
to /usr/local/bin
by hand!
$ pdiffjson
Usage: pdiffjson [--sort-arrays] [diff options] file1.json file2.json
Show pretty-printed, colored diff of normalized JSON. Uses less to
paginate the output. Allows a readable diff of any JSON, ignoring all
non-semantic differences of whitespace and key ordering.
Fields of each object are output with the keys in sorted order
prior to calling diff, so insignificant differences of key order
are ignored. The --sort-arrays option also enables recursive sorting
of all array values, so all differences in ordering within arrays
are ignored.
This is a "semi-structural diff", meaning it shows only structural
(semantic) changes but the diff is shown syntactically on JSON output,
which actually makes it more readable and flexible than a true
structural diff.
Works on large files since it relies only on diff and jq.
By default calls diff without arguments, yielding a unified diff. But
it can be helpful to add standard diff arguments to refine the type of
output, such as:
-c (contextual diff),
-C2 (contextual diff with two lines of context), or
-U5 (unified diff with 5 lines of context).
$