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.

Answer from Erik on Stack Overflow
🌐
GitHub
github.com › jlevy › pdiffjson
GitHub - jlevy/pdiffjson: View and diff JSON the easy way · GitHub
Use brew install jq colordiff on Mac, apt-get install jq colordiff or equivalent on Linux. Install the script anywhere you like. Simplest: ... 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.
Starred by 77 users
Forked by 6 users
Languages   Shell
Discussions

Comparing two json files : shell scripting - Stack Overflow
I would like to write a shell script which would compare the two line by line and whenever it finds a difference, output the line number where the difference occurred. ... To compare json files you should convert them so they have same order of keys. More on stackoverflow.com
🌐 stackoverflow.com
command line - CLI tool to compare 2 JSON files without regards to order of data objects AND returns an errorlevel environment variable - Software Recommendations Stack Exchange
I'm searching for a CLI (Windows) tool to compare 2 JSON files. The trick is that the data objects within the JSON files may be in different orders (positions). Even if the order of data objects c... More on softwarerecs.stackexchange.com
🌐 softwarerecs.stackexchange.com
windows - Offline JSON diff tool - Software Recommendations Stack Exchange
Either displays the difference directly (program has a GUI) or mangles the two JSON files in a way that a standard Difftool can compare it. ... linux has system "diff" command. I believe it is in all linux distros. It is offline no internet. More on softwarerecs.stackexchange.com
🌐 softwarerecs.stackexchange.com
Compare json files
There's the jq tool you can use to print out a json with sorted keys jq -S . A.json. Then compare the results with any text-diffing tool you like. On my linux I'd maybe do diff <(jq -S . A.json) <(jq -S . B.json) More on reddit.com
🌐 r/json
5
4
March 20, 2024
🌐
JSON Diff
jsondiff.com
JSON Diff - The semantic JSON compare tool
Validate, format, and compare two JSON documents. See the differences between the objects instead of just the new lines and mixed up properties.
🌐
GitHub
gist.github.com › ipan › e5e86d5495f16216e31fe12ebc9532a4
compare two JSONs with jq #json #jq · GitHub
You can use: diff <(jq 'keys' file1.json) <(jq 'keys' file2.json) This will just give you the list of keys that are different.
🌐
GitHub
github.com › josephburnett › jd
GitHub - josephburnett/jd: JSON diff and patch · GitHub
Same as -opts='[{"setkeys":["key1","key2"]}]'. -yaml Read and write YAML instead of JSON. -port=N Serve web UI on port N -precision=N Maximum absolute difference for numbers to be equal. Same as -opts='[{"precision":N}]'. Example: -precision=0.00001 -f=FORMAT Read and write diff in FORMAT "jd" (default), "patch" (RFC 6902) or "merge" (RFC 7386) -t=FORMATS Translate FILE1 between FORMATS.
Starred by 2.2K users
Forked by 65 users
Languages   Go 97.7% | Makefile 1.2%
🌐
Liquibase
docs.liquibase.com › documentation home › reference guide › database inspection, change tracking, and utility commands › diff json
diff JSON - Liquibase
The diff command in a JSON format ... as an input to automation processes. For example, the results in a JSON diff can be parsed in your build system to trigger alerts, generate reports, or run the diff-changelog command....
🌐
GitHub
github.com › andreyvit › json-diff
GitHub - andreyvit/json-diff: Structural diff for JSON files · GitHub
% json-diff --help Usage: json-diff [-vCjfonskKp] first.json second.json Arguments: <first.json> Old file <second.json> New file General options: -v, --verbose Output progress info -C, --[no-]color Colored output -j, --raw-json Display raw JSON encoding of the diff -f, --full Include the equal sections of the document, not just the deltas --max-elisions COUNT Max number of ...s to show in a row in "deltas" mode (before collapsing them) -o, --output-keys KEYS Always print this comma separated keys, with their value, if they are part of an object with any diff -x, --exclude-keys KEYS Exclude these comma separated keys from comparison on both files -n, --output-new-only Output only the updated and new key/value pairs (without marking them as such).
Starred by 1.2K users
Forked by 138 users
Languages   CoffeeScript 66.6% | JavaScript 33.4%
Find elsewhere
Top answer
1 of 3
1

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.

2 of 3
0

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.

🌐
GitHub
github.com › espadrine › json-diff
GitHub - espadrine/json-diff: Compute the difference between two JSON-serializable Ruby objects. · GitHub
Allows computing reverse operations without the source JSON. moves*: include move operations. Set it to false to remove clutter. additions*: include add operations. Set it to false to remove clutter. original_indices*: array indices are those from the source array (for from fields, or path fields on remove operations) or the target array (for other path fields). It eases manual checking of differences.
Starred by 80 users
Forked by 10 users
Languages   Ruby 99.6% | Makefile 0.4%
🌐
GitHub
github.com › jclulow › jsondiff
GitHub - jclulow/jsondiff: A simple command-line JSON diff utility
{ - aa: 7, + aa: 5, + b: false, - c: 6, + c: null, common: { - john: 4, + john: null, + mary: 5, still here: true }, + e: { + john: 5, + mary: 6, + stephen: 8 + }, equal: "!!!!", - removed: { - blue: false, - green: false, - red: true - }, stillisarray: [ - 1, - 1, - 2, - 5, 3, 4, 0, 2, 3, + 4, + 6, + 4, 2, 3, 5, 9 ], - wasarray: [ - 1, - 2, - 3, - 4 - ], + wasarray: { + test: "5" + }, - y: "diff all the things!", + y: "DIFF ALL THE THINGS!", z: true }
Starred by 62 users
Forked by 14 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
SourceForge
sourceforge.net › projects › json-diff.mirror
JSON-Diff download | SourceForge.net
August 22, 2025 - Download JSON-Diff for free. Structural diff for JSON files. json-diff is a command-line tool (and library) that computes differences between two JSON documents in a user-friendly manner. It highlights additions, deletions, and modifications in nested JSON structures, showing context so users ...
🌐
JSON Compare
jsoncompare.org
JSON Compare - Best JSON Diff Tools
It helps to Compare and find proper different in JSON Code, JSON files. It's also a JSON Beautify your compare Data. You can also download your JSON Data. Directly copy JSON Data and paste when you want. You can undo and redo your changes. Try with our sample data. JSON Compare working proper in Windows, Mac, Linux...
🌐
Medium
medium.com › @capeta1024 › json-diff-using-jq-vimdiff-b94829de40ff
JSON Diff using jq & vimdiff. JSON is a very commonly used data… | by Ankit Deshpande | Medium
September 26, 2022 - vimdiff staging-config-sorted.json prod-config-sorted.json · Vimdiff can be replaced by any other diff tool.
🌐
Genius Engineering
genius.engineering › faster-and-simpler-with-the-command-line-deep-comparing-two-5gb-json-files-3x-faster-by-ditching-the-code
Faster and simpler with the command line: deep-comparing two 5GB JSON files 3X faster by ditching the code
December 6, 2018 - Well first we need to sort these files so that tools like diff can easily compare them. But we can't just use sort; we need to sort them by the value of the genius_ids in their payload. It turns out this is quite easy with jq. To sort the exports by genius_id we can run: $ cat old_export.json | jq -csMS 'sort_by(.genius_id)[]' > sorted_old_export.json $ cat new_export.json | jq -csMS 'sort_by(.genius_id)[]' > sorted_new_export.json
🌐
Medium
lucasbru.medium.com › comparison-of-json-files-9b8d2fc320ca
Comparison of JSON files. Say, you want to compare to JSON files… | by Lucas Bruxxx | Medium
August 3, 2017 - cat X.json | jq -S -f walk.filter | 1.json cat Y.json | jq -S -f walk.filter | 2.json meld 1.json 2.json