xlwings
jsondiff
Python

Diff JSON and JSON-like structures in Python

Last updated Jul 2, 2026
749
Stars
90
Forks
22
Issues
+1
Stars/day
Attention Score
64
Language breakdown
Python 100.0%
โ–ธ Files click to expand
README

jsondiff

Diff JSON and JSON-like structures in Python.

Installation

`pip install jsondiff

Quickstart

<pre><code class="lang-python">&gt;&gt;&gt; import jsondiff as jd &gt;&gt;&gt; from jsondiff import diff

&gt;&gt;&gt; diff({&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;b&#39;: 3, &#39;c&#39;: 4}) {&#39;c&#39;: 4, &#39;b&#39;: 3, delete: [&#39;a&#39;]}

&gt;&gt;&gt; diff([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]) {insert: [(3, &#39;d&#39;)]}

&gt;&gt;&gt; diff([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;c&#39;]) {delete: [1]}

Typical diff looks like what you&#39;d expect...

&gt;&gt;&gt; diff({&#39;a&#39;: [0, {&#39;b&#39;: 4}, 1]}, {&#39;a&#39;: [0, {&#39;b&#39;: 5}, 1]}) {&#39;a&#39;: {1: {&#39;b&#39;: 5}}}

You can exclude some jsonpaths from the diff (doesn&#39;t work if the value types are different)

&gt;&gt;&gt; diff({&#39;a&#39;: 1, &#39;b&#39;: {&#39;b1&#39;: 20, &#39;b2&#39;: 21}, &#39;c&#39;: 3}, {&#39;a&#39;: 1, &#39;b&#39;: {&#39;b1&#39;: 22, &#39;b2&#39;: 23}, &#39;c&#39;: 30}, exclude_paths=[&#39;b.b1&#39;, &#39;c&#39;]) {&#39;b&#39;: {&#39;b2&#39;: 23}}

...but similarity is taken into account

&gt;&gt;&gt; diff({&#39;a&#39;: [0, {&#39;b&#39;: 4}, 1]}, {&#39;a&#39;: [0, {&#39;c&#39;: 5}, 1]}) {&#39;a&#39;: {insert: [(1, {&#39;c&#39;: 5})], delete: [1]}}

Support for various diff syntaxes

&gt;&gt;&gt; diff({&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;b&#39;: 3, &#39;c&#39;: 4}, syntax=&#39;explicit&#39;) {insert: {&#39;c&#39;: 4}, update: {&#39;b&#39;: 3}, delete: [&#39;a&#39;]}

&gt;&gt;&gt; diff({&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;b&#39;: 3, &#39;c&#39;: 4}, syntax=&#39;symmetric&#39;) {insert: {&#39;c&#39;: 4}, &#39;b&#39;: [2, 3], delete: {&#39;a&#39;: 1}}

&gt;&gt;&gt; diff({&#39;list&#39;: [1, 2, 3], &quot;poplist&quot;: [1, 2, 3]}, {&#39;list&#39;: [1, 3]}, syntax=&quot;rightonly&quot;) {&quot;list&quot;: [1, 3], delete: [&quot;poplist&quot;]}

Special handling of sets

&gt;&gt;&gt; diff({&#39;a&#39;, &#39;b&#39;, &#39;c&#39;}, {&#39;a&#39;, &#39;c&#39;, &#39;d&#39;}) {discard: set([&#39;b&#39;]), add: set([&#39;d&#39;])}

Load and dump JSON

&gt;&gt;&gt; print diff(&#39;[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]&#39;, &#39;[&quot;a&quot;, &quot;c&quot;, &quot;d&quot;]&#39;, load=True, dump=True) {&quot;$delete&quot;: [1], &quot;$insert&quot;: [[2, &quot;d&quot;]]}

NOTE: Default keys in the result are objects, not strings!

&gt;&gt;&gt; d = diff({&#39;a&#39;: 1, &#39;delete&#39;: 2}, {&#39;b&#39;: 3, &#39;delete&#39;: 4}) &gt;&gt;&gt; d {&#39;delete&#39;: 4, &#39;b&#39;: 3, delete: [&#39;a&#39;]} &gt;&gt;&gt; d[jd.delete] [&#39;a&#39;] &gt;&gt;&gt; d[&#39;delete&#39;] 4

Alternatively, you can use marshal=True to get back strings with a leading $

&gt;&gt;&gt; diff({&#39;a&#39;: 1, &#39;delete&#39;: 2}, {&#39;b&#39;: 3, &#39;delete&#39;: 4}, marshal=True) {&#39;delete&#39;: 4, &#39;b&#39;: 3, &#39;$delete&#39;: [&#39;a&#39;]}</code></pre>

Command Line Client

Usage: <pre><code class="lang-">jdiff [-h] [-p] [-s {compact,symmetric,explicit}] [-i INDENT] [-f {json,yaml}] first second

positional arguments: first second

optional arguments: -h, --help show this help message and exit -p, --patch -s {compact,symmetric,explicit}, --syntax {compact,symmetric,explicit} Diff syntax controls how differences are rendered (default: compact) -i INDENT, --indent INDENT Number of spaces to indent. None is compact, no indentation. (default: None) -f {json,yaml}, --format {json,yaml} Specify file format for input and dump (default: json)</code></pre>

Examples:

<pre><code class="lang-bash">$ jdiff a.json b.json -i 2

$ jdiff a.json b.json -i 2 -s symmetric

$ jdiff a.yaml b.yaml -f yaml -s symmetric</code></pre>

Development

Install development dependencies and test locally with

<pre><code class="lang-bash">pip install -r requirements-dev.txt

... do your work ... add tests ...

pytest</code></pre>

Installing From Source

To install from source run

<pre><code class="lang-bash">pip install .</code></pre>

This will install the library and cli for jsondiff` as well as its runtime dependencies.

Testing before release

python -m build
twine check dist/*

ยฉ 2026 GitRepoTrend ยท xlwings/jsondiff ยท Updated daily from GitHub