If your shell supports process substitution (Bash-style follows, see docs):
diff <(jq --sort-keys . A.json) <(jq --sort-keys . B.json)
Objects key order will be ignored, but array order will still matter. It is possible to work-around that, if desired, by sorting array values in some other way, or making them set-like (e.g. ["foo", "bar"] → {"foo": null, "bar": null}; this will also remove duplicates).
Alternatively, substitute diff for some other comparator, e.g. cmp, colordiff, or vimdiff, depending on your needs. If all you want is a yes or no answer, consider using cmp and passing --compact-output to jq to not format the output for a potential small performance increase.
If your shell supports process substitution (Bash-style follows, see docs):
diff <(jq --sort-keys . A.json) <(jq --sort-keys . B.json)
Objects key order will be ignored, but array order will still matter. It is possible to work-around that, if desired, by sorting array values in some other way, or making them set-like (e.g. ["foo", "bar"] → {"foo": null, "bar": null}; this will also remove duplicates).
Alternatively, substitute diff for some other comparator, e.g. cmp, colordiff, or vimdiff, depending on your needs. If all you want is a yes or no answer, consider using cmp and passing --compact-output to jq to not format the output for a potential small performance increase.
Use jd with the -set option:
No output means no difference.
$ jd -set A.json B.json
Differences are shown as an @ path and + or -.
$ jd -set A.json C.json
@ ["People",{}]
+ "Carla"
The output diffs can also be used as patch files with the -p option.
$ jd -set -o patch A.json C.json; jd -set -p patch B.json
{"City":"Boston","People":["John","Carla","Bryan"],"State":"MA"}
https://github.com/josephburnett/jd#command-line-usage
Videos
» npm install json-diff-kit
» npm install json-diff-cli
You can run both files through jq (available as a small Windows binary), let jq pretty-print and sort the data in both files according to your needs, and then perform an "ordinary" diff (with WinDiff, Meld, Diffuse).
the shell commands would be
< file1.json jq 'keys' > file1.sorted.json
< file2.json jq 'keys' > file2.sorted.json
diff file1.sorted.json file2.sorted.json
# or better
diff -q file1.sorted.json file2.sorted.json
#
# -q, --brief
# report only when files differ
# if output of diff -q is nonempty, files differ -> raise an error
If you know what's going on, continue with the unmodified files.
This obviously works only if the JSON objects differ only in their keys. This also assumes that the type info is preserved (e.g. the string representation of floats does not differ between the two files)
If the JSON objects differ in object values, or if the values contain nested objects (which themselves can differ in values or in subkey ordering), or if you want the order of the first-level-keys unchanged, but need to only compare the nested "value-objects" by some other criterion, you will need more complicated jq commands.
Check stackoverflow.com - some incredible jq experts there.
Did you try and take a look at GNU Diffutils (diff)? Diff specifically has the "-B" or "--ignore-blank-lines" switch, which "Ignore changes whose lines are all blank" (see also a similar question on Stack Exchange).
There are other ways and variants as well to achieve the things you mentioned, such as using Vimdiff for example.
If you want to take a programmatic approach, you might want to take a look at the Levenshtein distance, which is a metric for measuring the difference between two Strings.
» npm install json-diff
Use jq to first sort all keys. Then diff to do the diffing:
jq -S . A.json > A-sorted.json
jq -S . B.json > B-sorted.json
diff A-sorted.json B-sorted.json
The above example is for Linux but both jq and diff is available for Windows.
It seems Windows has an alternative diff tool called fc, perhaps this could be used instead of diff.
QT JSON Diff [Github] might be for you:
- They have a Windows build (see "Win64" in the releases)
- It is gratis, MIT license
- It works offline, as an EXE which runs locally
- can sort objects inside arrays so that order matters less
Hey r/golang community,
I've been grappling with a recurring challenge in my work: comparing large, deeply nested JSON files. To tackle this, I created a CLI tool in Go that helps highlight differences between two JSON files with high precision.
The tool is relatively compact, about 200 lines of code, and handles different types of changes well, including additions, deletions, and type mismatches. However, I'm looking to improve how differences within arrays are displayed. Currently, if an item at the beginning of an array is modified or removed, all subsequent items are marked as changed, which isn't very helpful for quick analysis.
Here's where I could use your collective wisdom:
Visualization Techniques: Does anyone have suggestions for more intuitive ways to display array differences? Maybe a method that effectively pinpoints and isolates changes without cascading the entire array?
Code Feedback: I'd love to get some feedback on the implementation. I'm sure there are optimizations and best practices that I could apply to enhance the tool's performance and usability.
I'm looking forward to your insights and suggestions. Let's make JSON comparisons easier for everyone!
You can check out the tool and contribute here: https://github.com/phplego/jcmp
Thank you all in advance!