- Just the simplest and fastest way to format, display, and diff JSON directly from the command line. Not a fancy web UI or a library.
pjson
andpdiffjson
are simple bash scripts that are useful (and non-obvious) enough more developers should have them at their fingertips. They combinejq
,diff
,colordiff
, andless
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.- Display and diff both ignore all non-semantic differences in formatting and whitespace. Only significant differences are shown in the diff output.
- The ordering of keys on collections objects is always ignored (as it should be!) by sorting keys. There’s an option to sort arrays, too, to allow unordered comparison of lists.
- Works on large files other tools barf on, thanks to
jq
’s speed. - Diff shows context around each change, thanks to
diff
. - Scrollable and searchable output (use space,
/
), thanks toless
. - Installs quickly anywhere, thanks to
npm
. - The feature list is longer than the code! 🤯😀
-
Ensure you have
jq
andcolordiff
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
andpdiffjson
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).
$
- Many websites like jsondiff offer JSON diff functionality but offer no command line, don’t work on large files, and are not good for sensitive data.
- jsondiffpatch is a powerful JavaScript library to do JSON diff and patch. Has a command line. Shows a pure structural diff. No semi-structured diff with context.
- diff-json is another useful JavaScript library with a command-line interface. Rather slow on larger (multi-megabyte) files, with no incremental output.
- jd is a powerful Go command line tool. It handles diff and patching. It allows more flexible diffs options on arrays (set and bag). No colored output or diff output options to show more context, as with diff, so the output seems less clear to read.