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

Faster and simpler with the command line: deep-comparing JSON files with jq
Btw I'm surprised you needed -M, since I thought jq would suppress colors if it saw it wasn't writing to a tty · Even when reading the article I thought about it :) More on news.ycombinator.com
🌐 news.ycombinator.com
84
206
December 17, 2018
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
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
Because it will be running from ... if the 2 JSON files are equivalent. CLI is really what I prefer, but a GUI that conforms to the above will work too. So please don't hesitate to recommend either. ... Wait a minute, by "different positions" do you only mean dictionaries? Because in lists a different order may be quite intentional even though the items contained on either side may refer to the same set of items. Lists are ordered, after all. ... You can run both files through jq (available ... More on softwarerecs.stackexchange.com
🌐 softwarerecs.stackexchange.com
How to compare keys of two json documents?
Maybe this? diff <(gron --no-sort a.json) <(gron --no-sort b.json) You need to install gron for this More on reddit.com
🌐 r/bash
9
0
July 30, 2024
🌐
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 - cat staging-config.json | jq -S >> staging-config-sorted.json cat prod-config.json | jq -S >> prod-config-sorted.json · Step 3: Use vimdiff to see the difference between the two files
🌐
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.
🌐
Hacker News
news.ycombinator.com › item
Faster and simpler with the command line: deep-comparing JSON files with jq | Hacker News
December 17, 2018 - Btw I'm surprised you needed -M, since I thought jq would suppress colors if it saw it wasn't writing to a tty · Even when reading the article I thought about it :)
🌐
Ente
ente.io › home › blog › an introduction to the magic of jq
An introduction to the magic of jq
May 31, 2022 - We just want the value without the quotes, so let us ask jq to give us raw output by specifying the "--raw-output" flag (or its shorthand, "-r"). $ scw instance security-group list --output json name=scg-prod | jq -r 'first.id' xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
🌐
Serhii
serhii.net › dtb › 230313-1423-json-diff-with-jq
json diff with jq, also: side-by-side output - serhii.net
March 13, 2023 - JSON Diff - The semantic JSON compare tool is a bit prettier All similar to each other. I don’t find them intuitive, don’t like copypasting, and privacy/NDAs are important. SO thread1 version: diff <(jq --sort-keys . A.json) <(jq --sort-keys . B.json) Wrapped it into a function in my .zshrc: jdiff() { diff <(jq --sort-keys .
Find elsewhere
🌐
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
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.

🌐
Substack
codefaster.substack.com › p › json-toolkit-json-diff
json-diff - by Tyler Adams - CodeFaster
December 29, 2020 - In this post, we’ll explore json-diff (a tool from my json-toolkit), how to use it and how to write programs that use its output.
🌐
Medium
medium.com › @lucasbru › comparison-of-json-files-9b8d2fc320ca
Comparison of JSON files. Say, you want to compare to JSON files… | by Lucas Bruxxx | Medium
August 3, 2017 - You have a directory full of json files to compare? The procedure does not change much · mkdir 1 mkdir 2 for i in `ls X/`; do cat X/$i | jq -S -f walk.filter > 1/$i; done for i in `ls Y/`; do cat Y/$i | jq -S -f walk.filter > 2/$i; done meld 1/ 2/
🌐
Nilorea
nilorea.net › 2022 › 01 › 20 › compare-two-json-in-shell
Compare two json in shell – Nilorea Studio
January 20, 2022 - users/$user) <(jq -S . updated-users/$user) # or full on one side and the diff on the other side, colored # diff -y --left-column --color <(jq -S . users/$user) <(jq -S .
🌐
LinkedIn
linkedin.com › pulse › fuzzy-snapshot-testing-jq-diff-measures-for-justice-institute
Fuzzy snapshot testing with jq and diff
March 28, 2023 - We used a graphical diff app as we went through each step, both to investigate differences found between old and new systems, and to verify the effects of each jq transformation. Interactive comparisons handled, we wanted a way to automate it so a script could tell us if every difference was accounted for. By default the `diff` command will output a patch that shows what all the differences are: diff before4.json after4.json 5c5 < "sources": [8, 9], --- > "sources": [],
🌐
GitHub
gist.github.com › wader › 4163da796082c3ddaf32e4602547c604
json diff with jq · GitHub
json diff with jq. GitHub Gist: instantly share code, notes, and snippets.
🌐
Leapcell
leapcell.io › blog › how-to-compare-json-a-practical-guide
How to Compare JSON: A Practical Guide | Leapcell
July 25, 2025 - jq: A lightweight and flexible command-line JSON processor. You can use it to sort and format JSON data before comparison. diff: A standard Unix command-line tool that compares files line by line.
🌐
GitHub
github.com › jlevy › pdiffjson
GitHub - jlevy/pdiffjson: View and diff JSON the easy way
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 100.0% | Shell 100.0%