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
๐ŸŒ
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.
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ json-diff
json-diff - npm
-s, --sort Sort primitive values in arrays before comparing -k, --keys-only Compare only the keys, ignore the differences in values -K, --keep-unchanged-values Instead of omitting values that are equal, output them as they are -p, --precision DECIMALS Round all floating point numbers to this number of decimal places prior to comparison -h, --help Display this usage information ... var jsonDiff = require('json-diff'); console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' })); // Output: // { // - foo: "bar" // + foo: "baz" // } // As above, but without console colors console.log(jsonDiff
      ยป npm install json-diff
    
Published ย  May 15, 2023
Version ย  1.0.6
Author ย  Andrey Tarantsov
Discussions

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
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
windows - Offline JSON diff tool - Software Recommendations Stack Exchange
Unicode would not matter in this ... through drag-and-drop, normalizes JSON (e.g. using json-cpp) creating two temporary files and launches some diff tool (WinMerge maybe?) passing temporary file names to it. ... Use jq to first sort all keys.... More on softwarerecs.stackexchange.com
๐ŸŒ softwarerecs.stackexchange.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
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
๐ŸŒ
Leapcell
leapcell.io โ€บ blog โ€บ how-to-compare-json-a-practical-guide
How to Compare JSON: A Practical Guide | Leapcell
July 25, 2025 - diff: A standard Unix command-line tool that compares files line by line. When used with jq, it can compare JSON files effectively. ... This approach sorts the JSON files to ensure consistent key order before comparison.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
Js
json-diff-kit.js.org
JSON Diff Kit Playground
We cannot provide a description for this page right now
๐ŸŒ
GitHub
gist.github.com โ€บ wader โ€บ 4163da796082c3ddaf32e4602547c604
json diff with jq ยท GitHub
json diff with jq ยท Raw ยท diff.jq ยท This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ยท
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ keploy โ€บ diff-json-a-complete-guide-to-comparing-json-data-3e31
Diff JSON โ€“ A Complete Guide to Comparing JSON Data - DEV Community
October 15, 2024 - JSON Diff Tools and Libraries ... directly in the terminal. โ€ข JSON-diff (npm library): This JavaScript package helps compare JSON objects and outputs differences....
๐ŸŒ
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 :)
๐ŸŒ
codestudy
codestudy.net โ€บ blog โ€บ how-can-i-completely-sort-arbitrary-json-using-jq
How to Completely Sort Arbitrary JSON with jq (Including Arrays) for Accurate Diff Comparisons โ€” codestudy.net
By sorting objects and arrays, we eliminate these false differences, making diff comparisons meaningful. jq is a lightweight, command-line JSON processor that lets you filter, transform, and manipulate JSON data with expressive syntax.
๐ŸŒ
JSDiff
json.jsdiff.com โ€บ home
JSON Diff - The semantic JSON compare tool - JavaScript Text Diff Library | Myers Algorithm Implementation
JSDiff is a free online tool designed specifically for developers. It helps you quickly compare the differences between two segments of JSON or JavaScript code, supporting highlighted display, structured presentation, and one-click merging.
๐ŸŒ
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": [],
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 โ€บ andreyvit โ€บ json-diff
GitHub - andreyvit/json-diff: Structural diff for JSON files ยท GitHub
-s, --sort Sort primitive values in arrays before comparing -k, --keys-only Compare only the keys, ignore the differences in values -K, --keep-unchanged-values Instead of omitting values that are equal, output them as they are -p, --precision DECIMALS Round all floating point numbers to this number of decimal places prior to comparison -h, --help Display this usage information ... var jsonDiff = require('json-diff'); console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' })); // Output: // { // - foo: "bar" // + foo: "baz" // } // As above, but without console colors console.log(jsonDiff
Starred by 1.2K users
Forked by 138 users
Languages ย  CoffeeScript 66.6% | JavaScript 33.4%
๐ŸŒ
DEV Community
dev.to โ€บ keploy โ€บ compare-two-json-objects-with-jackson-32d2
Compare Two JSON Objects with Jackson - DEV Community
June 13, 2024 - Command-line tools like diff and jq offer efficient ways to compare JSON files. diff provides a line-by-line comparison, while jq allows for sorting keys and extracting specific elements before comparison.
๐ŸŒ
DEV Community
dev.to โ€บ keploy โ€บ diff-json-simplifying-json-comparisons-1fm6
Diff JSON: Simplifying JSON Comparisons - DEV Community
January 16, 2025 - Integration: Tools like jq excel in CI/CD pipelines. ... While tools and libraries are helpful, manual JSON comparison can sometimes be necessary for debugging or edge cases. Hereโ€™s how you can do it: Normalize the Data: Ensure both JSON objects are formatted consistently (e.g., sort keys alphabetically). Compare Key-Value Pairs: Start with top-level keys and drill down into nested structures. Use Visual Diffing Tools: Text editors with diff features (e.g., VS Code, Sublime Text) can help visualize changes.