0.3.0 • Published 4 years ago

pdiffjson v0.3.0

Weekly downloads
10
License
Apache 2
Repository
github
Last release
4 years ago

pdiffjson

  • pdiffjson is a one-liner bash script that’s non-obvious and useful enough more developers should have it at their fingertips. It simply combines jq, diff, colordiff, and less to normalize and then diff.
  • One of the simplest and fastest ways to get prettily printed, colored, paginated diffs of JSON files. Ignores all irrelevant whitespace and key ordering on collections objects. Just shows you visually what’s different between two files.
  • Not a fancy web UI or a library.
  • Works on large files other tools barf on, thanks to jq’s speed.
  • Shows context around each difference in a customizable way, thanks to diff.
  • Scrollable and searchable output (use space, /), thanks to less.
  • Installs quickly anywhere, thanks to bash.
  • Extra bonus pjson command that colorizes and pretty-prints only.
  • The feature list is longer than the code! 🤯😀

example usage

Installation

$ npm install -g pdiffjson

Or copy the damn bash file to /usr/local/bin by hand. Also ensure you have jq and colordiff as well (brew install jq colordiff on Mac).

Usage

$ 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).

$

Related Work

  • 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.