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.
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
Feature: Add a "diff" built-in function/filter/operator
I am not an expert of jq and giving an implementation suggestion is far from my possibilities. But I tested quickly json_diff from a python implementation: it takes a very long time for even short files, and jd which is indeed relatively fast. More on github.com
🌐 github.com
4
December 28, 2016
🌐
Topanswers
topanswers.xyz › nix
how can I diff two json files with jq? - *nix - TopAnswers
You can diff two files in a single command by getting the sorted full paths (including values) for each leaf node: ```shell diff \ <(echo '{ "a": "foo", "b": [ 1, 2, { "d": "bar" } ] }' | jq -S | jq -c 'paths(.==null or scalars) as $path | getpath($path) as $v | $path | [.,$v]') \ <(echo '{ "b": [ 1, 2, { "d": "baz" } ], "a": "foo" }' | jq -S | jq -c 'paths(.==null or scalars) as $path | getpath($path) as $v | $path | [.,$v]') 4c4 < [["b",2,"d"],"bar"] --- > [["b",2,"d"],"baz"] ``` The path strings can then be used to traverse the original JSON in `jq` again if required: ```shell echo '{ "a": "foo", "b": [ 1, 2, { "d": "bar" } ] }' | jq 'getpath(["b",2,"d"])' "bar" ```
🌐
GitHub
gist.github.com › zfz › 0932119200d2c94760019d85af2fb6fe
compare two JSONs with jq #json #jq · GitHub
diff <(python -m json.tool --sort-keys carting.avsc) <(python -m json.tool --sort-keys carting7.avsc)
🌐
Substack
codefaster.substack.com › p › json-toolkit-json-diff
json-diff - by Tyler Adams - CodeFaster
December 29, 2020 - $ echo '{"k": "v"}' > kv.json $ echo '{"k": "o"}' > ko.json $ json-diff kv.json ko.json [{"leftValue":"v","path":["k"],"rightValue":"o"}] We can also pretty print the output by piping the output to jq.
🌐
Leapcell
leapcell.io › blog › how-to-compare-json-a-practical-guide
How to Compare JSON: A Practical Guide | Leapcell
July 25, 2025 - Use online tools like JSON Diff for quick visual comparisons. To prevent differences caused by key order rather than content. Yes, using libraries like deepdiff in Python or scripts with jq.
🌐
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 :)
Find elsewhere
🌐
Keploy
keploy.io › home › community › how to compare two json files?
How to compare two JSON files? | Keploy Blog
June 9, 2024 - Find differences between two JSON files using Python, VS Code, and free online JSON diff tools. Ideal for developers and testers.
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.

🌐
Jacob Emcken
emcken.dk › programming › 2024 › 04 › 07 › compare-json-files-offline
How to compare JSON files offline
jq -S . A.json > A-sorted.json jq -S . B.json > B-sorted.json diff A-sorted.json B-sorted.json
🌐
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
🌐
GitHub
github.com › jqlang › jq › issues › 1381
Feature: Add a "diff" built-in function/filter/operator · Issue #1381 · jqlang/jq
December 28, 2016 - But I think it gives the idea of the potential of such an operator/filter/function. Beside a "local" version, a fully jq defined "json" diff can be implemented by simply merging 2 json files as a 2 dimensional array and indeed do: .[0] diff .[1].
Author   mariotti
Top answer
1 of 3
21

Check out this python library jsondiff , that will help you to identify the diff's

import json

import jsondiff

json1 = json.loads(
    '{"isDynamic": false, "name": "", "value": "SID:<sid>", "description": "instance","argsOrder": 1,"isMultiSelect": false}')

json2 = json.loads(
    '{ "name": "", "value": "SID:<sid>","isDynamic": false, "description": "instance","argsOrder": 1,"isMultiSelect": false}')

res = jsondiff.diff(json1, json2)
if res:
    print("Diff found")
else:
    print("Same")
2 of 3
6

UPDATED: See https://eggachecat.github.io/jycm-json-diff-viewer/ for a live demo! Now it has a JS-native implementation.

Affiliation: I am the author of this lib.

Yes! You can diff it with jycm which has a rendering tool out of the box.

It uses LCS, Edit distance and Kuhn–Munkres to diff arrays.

Here's an universal example with set in set and value changes in some set

from jycm.helper import make_ignore_order_func
from jycm.jycm import YouchamaJsonDiffer

left = {
    "set_in_set": [
        {
            "id": 1,
            "label": "label:1",
            "set": [
                1,
                5,
                3
            ]
        },
        {
            "id": 2,
            "label": "label:2",
            "set": [
                4,
                5,
                6
            ]
        }
    ]
}

right = {
    "set_in_set": [
        {
            "id": 2,
            "label": "label:2",
            "set": [
                6,
                5,
                4
            ]
        },
        {
            "id": 1,
            "label": "label:1111",
            "set": [
                3,
                2,
                1
            ]
        }
    ]
}

ycm = YouchamaJsonDiffer(left, right, ignore_order_func=make_ignore_order_func([
    "^set_in_set$",
    "^set_in_set->\\[\\d+\\]->set$"
]))

ycm.diff()

expected = {
    'list:add': [
        {'left': '__NON_EXIST__', 'right': 2, 'left_path': '', 'right_path': 'set_in_set->[1]->set->[1]'}
    ],
    'list:remove': [
        {'left': 5, 'right': '__NON_EXIST__', 'left_path': 'set_in_set->[0]->set->[1]', 'right_path': ''}
    ],
    'value_changes': [
        {'left': 'label:1', 'right': 'label:1111', 'left_path': 'set_in_set->[0]->label',
         'right_path': 'set_in_set->[1]->label', 'old': 'label:1', 'new': 'label:1111'}
    ]
}

assert ycm.to_dict(no_pairs=True) == expected

you can set no_pairs=False to get the all pairs. Here's a rendered example:

As for the example here, you can use it as:

from jycm.helper import make_ignore_order_func
from jycm.jycm import YouchamaJsonDiffer

left = {
    "data": [{"x": 1, "y": 2}, {"x": 3, "y": 4}]
}

right = {
    "data": [{"x": 3, "y": 4}, {"x": 1, "y": 2}]
}

ycm = YouchamaJsonDiffer(left, right, ignore_order_func=make_ignore_order_func([
    "^data",
]))

ycm.diff()

assert ycm.to_dict(no_pairs=True) == {}

Bonus, you the values are interrupted as coordinates on plain, you can even define a operator to determine whether two points should be matched!(Then comparing their values)

Here's the code:

from typing import Tuple

from jycm.helper import make_ignore_order_func
from jycm.jycm import YouchamaJsonDiffer
from jycm.operator import BaseOperator
import math

left = {
    "data": [
        {"x": 1, "y": 1},
        {"x": 10, "y": 10},
        {"x": 100, "y": 100}
    ]
}

right = {
    "data": [
        {"x": 150, "y": 150},
        {"x": 10, "y": 11},
        {"x": 2, "y": 3}
    ]
}


class L2DistanceOperator(BaseOperator):
    __operator_name__ = "operator:l2distance"
    __event__ = "operator:l2distance"

    def __init__(self, path_regex, distance_threshold):
        super().__init__(path_regex=path_regex)
        self.distance_threshold = distance_threshold

    def diff(self, level: 'TreeLevel', instance, drill: bool) -> Tuple[bool, float]:
        distance = math.sqrt(
            (level.left["x"] - level.right["x"]) ** 2 + (level.left["y"] - level.right["y"]) ** 2
        )
        info = {
            "distance": distance,
            "distance_threshold": self.distance_threshold,
            "pass": distance < self.distance_threshold
        }

        if not drill:
            instance.report(self.__event__, level, info)
            return False, 1 if info["pass"] else 0
        return True, 1 if info["pass"] else 0


ycm = YouchamaJsonDiffer(left, right, ignore_order_func=make_ignore_order_func([
    "^data$",
]), custom_operators=[
    L2DistanceOperator("^data->\\[.*\\]$", 10),
])

ycm.diff()

expected = {
    'just4vis:pairs': [
        {'left': 1, 'right': 2, 'left_path': 'data->[0]->x', 'right_path': 'data->[2]->x'},
        {'left': {'x': 1, 'y': 1}, 'right': {'x': 2, 'y': 3}, 'left_path': 'data->[0]',
         'right_path': 'data->[2]'},
        {'left': 1, 'right': 3, 'left_path': 'data->[0]->y', 'right_path': 'data->[2]->y'},
        {'left': {'x': 1, 'y': 1}, 'right': {'x': 2, 'y': 3}, 'left_path': 'data->[0]',
         'right_path': 'data->[2]'},
        {'left': {'x': 1, 'y': 1}, 'right': {'x': 2, 'y': 3}, 'left_path': 'data->[0]',
         'right_path': 'data->[2]'}
    ],
    'list:add': [
        {'left': '__NON_EXIST__', 'right': {'x': 150, 'y': 150}, 'left_path': '', 'right_path': 'data->[0]'}
    ],
    'list:remove': [
        {'left': {'x': 100, 'y': 100}, 'right': '__NON_EXIST__', 'left_path': 'data->[2]', 'right_path': ''}
    ],
    'operator:l2distance': [
        {'left': {'x': 1, 'y': 1}, 'right': {'x': 2, 'y': 3}, 'left_path': 'data->[0]',
         'right_path': 'data->[2]', 'distance': 2.23606797749979, 'distance_threshold': 10,
         'pass': True},
        {'left': {'x': 10, 'y': 10}, 'right': {'x': 10, 'y': 11}, 'left_path': 'data->[1]',
         'right_path': 'data->[1]', 'distance': 1.0, 'distance_threshold': 10,
         'pass': True}
    ],
    'value_changes': [
        {'left': 1, 'right': 2, 'left_path': 'data->[0]->x', 'right_path': 'data->[2]->x', 'old': 1, 'new': 2},
        {'left': 1, 'right': 3, 'left_path': 'data->[0]->y', 'right_path': 'data->[2]->y', 'old': 1, 'new': 3},
        {'left': 10, 'right': 11, 'left_path': 'data->[1]->y', 'right_path': 'data->[1]->y', 'old': 10, 'new': 11}
    ]
}
assert ycm.to_dict() == expected

As you can see jycm report addition and remove for points {'x': 150, 'y': 150} and {'x': 100, 'y': 100} for their distances are too far (more than 10) and value-change for the other two points.

P.S. RENDERER DEMO

🌐
Reddit
reddit.com › r/learnpython › comparing two json files and outputting the difference in a readable json format?
r/learnpython on Reddit: Comparing two JSON files and outputting the difference in a readable JSON format?
August 22, 2017 -

Hi all,

I posted on this yesterday but didn't end up getting anywhere. I thought I'd post again with some other ideas and see how I go.

Scenario:

I've got two JSON files pulled from the same website, which I'm importing a day apart using json.dump with indent=4.

I would like to compare the old JSON file with the new JSON file with the possibility that the newer file will have additional objects.

The only problem is, it needs to be readable as a JSON so I can print/use the values.

The only solution that has come close so far is to try and format the file first, then use subprocess and bash to run:

def bash_command(cmd):
    subprocess.Popen(cmd, shell=True, executable='/bin/bash')

bash_command('comm -3 markets.json updatemarkets.json >> newpairs.json')

But obviously this does not format it correctly.

I'm sure there's a Python solution which isn't hacked together like the above.

I've looked into Python libraries like difflib, as well as other bash options like diff and jq, but none seem to give me what I want in an efficient way.

Let me know what you think!

🌐
GitHub
gist.github.com › wader › 4163da796082c3ddaf32e4602547c604
json diff with jq · GitHub
json diff with jq. GitHub Gist: instantly share code, notes, and snippets.
🌐
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
In this guide, we’ll explore how to use jq to recursively sort arbitrary JSON (including nested objects, arrays, and mixed data types) for accurate diff comparisons. ... JSON specifications (e.g., RFC 8259) state that object key order is not guaranteed—parsers may return keys in any order. However, in practice, most JSON generators (e.g., Python’s json.dump, JavaScript’s JSON.stringify) preserve insertion order.
🌐
GitHub
github.com › parjun8840 › jsondiff
GitHub - parjun8840/jsondiff: Json diff using python
C:\Users\parjun8840\eclipse-workspace\PythonDev>python jsondiff.py --help usage: jsondiff.py [-h] [-f1 FILE1_PATH] [-f2 FILE2_PATH] JSON DIFF optional arguments: -h, --help show this help message and exit -f1 FILE1_PATH, --FILE1_PATH FILE1_PATH First Json File -f2 FILE2_PATH, --FILE2_PATH FILE2_PATH Second Json file C:\Users\parjun8840\eclipse-workspace\PythonDev> Scenario 1: Both files same content Input files used: json_dict1 ,json_dict1 have same content {"test1": "1", "test2" : "2", "test3" : "abcd", "test4" : "xyz", "test5" : "12ab", "test6" : "12ab" } C:\Users\parjun8840\eclipse-workspace\PythonDev>python jsondiff.py -f1 json_dict1 -f2 json_dict2 INFO: Files exists and non-empty JSON files are same C:\Users\parjun8840\eclipse-workspace\PythonDev> Scenario 2: When one of the file has extra key and other one has different value for the same key.
Author   parjun8840
🌐
GitHub
github.com › jlevy › pdiffjson
GitHub - jlevy/pdiffjson: View and diff JSON the easy way
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.
Starred by 77 users
Forked by 6 users
Languages   Shell 100.0% | Shell 100.0%