Skip to main content

Command line program & Python functions for comparing multiple dictionaries

Python Package Index Build Status Test Coverage

dictsdiff provides a CLI and Python interface for comparing arbitrary number of nested dictionaries and show it as a tabular format via pandas.DataFrame.

CLI

Usage:

dictsdiff FILE [JSON_PATH] [FILE [JSON_PATH] ...]
dictsdiff --ndjson=FILE.ndjson
cat *.ndjson | dictsdiff [--ndjson=-]

When paths to multiple files are given, it loads the dictionaries from those files and compare (possibly) nested values in them. The key-value pairs that are different or missing are shown in a table format. A file path FILE may be followed by a JSONPath JSON_PATH which starts with $.. If FILE starts with $., prepend ./ to FILE to disambiguate the argument. JSON_PATH can be used for non-JSON files.

When no files are given, it is assumed that Newline delimited JSON (ndjson) is fed to the stdin.

Examples

$ echo '{"a": 1, "b": {"c": 0, "d": 0, "e": 0}}' > 0.json
$ echo '{"a": 2, "b": {"c": 0, "d": 1, "e": 0}}' > 1.json
$ echo '{"a": 2, "b": {"c": 0, "d": 1}}' > 2.json
$ dictsdiff *.json
        a  b.d  b.e
path
0.json  1    0  0.0
1.json  2    1  0.0
2.json  2    1  NaN
$ cat *.json | dictsdiff
   a  b.d  b.e
0  1    0  0.0
1  2    1  0.0
2  2    1  NaN

If JSON files are pre-processed by jq, dictsdiff can handle its output when --compact-output/-c is passed:

jq --compact-output '' **/*.json | dictsdiff

To pass the original file path of JSON files to dictsdiff, use --info-key option combined with jq’s input_filename, e.g.,:

jq --compact-output '.path = input_filename' **/*.json \
  | dictsdiff --info-key=path

Python interface

dictsdiff.diff_dicts

>>> from dictsdiff import diff_dicts
>>> dd = diff_dicts([
...     {'a': 1, 'b': {'c': 0, 'd': 0}},
...     {'a': 2, 'b': {'c': 0, 'd': 1}},
...     {'a': 1, 'b': {'c': 0, 'd': 1}},
... ])
>>> dd.keys
[('a',), ('b', 'd')]
>>> dd.pretty_diff()
   a  b.d
0  1    0
1  2    1
2  1    1

dictsdiff.diff_files

>>> from dictsdiff import diff_files
>>> _ = open('0.json', 'w').write('{"a": 1, "b": 2}')
>>> _ = open('1.json', 'w').write('{"a": 1, "b": 3}')
>>> dd = diff_files(['0.json', '1.json'])
>>> dd.keys
[('b',)]
>>> dd.pretty_diff()
        b
path
0.json  2
1.json  3

dictsdiff.diff_ndjson

>>> import io
>>> from dictsdiff import diff_ndjson
>>> ndjson = u'''
... {"a": 1, "b": {"c": 0, "d": 0}}
... {"a": 2, "b": {"c": 0, "d": 1}}
... '''.strip()
>>> dd = diff_ndjson(io.StringIO(ndjson))
>>> dd.keys
[('a',), ('b', 'd')]
>>> dd.pretty_diff()
   a  b.d
0  1    0
1  2    1

Installation

pip install dictsdiff  # or
pip install https://github.com/tkf/dictsdiff/archive/master.zip

Requirements

Metadata

Release files for dictsdiff 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dictsdiff 0.1.1
File Size Uploaded Provenance Actions
dictsdiff-0.1.1.tar.gz 12.5 kB Without provenance Details for dictsdiff-0.1.1.tar.gz

Release files / dictsdiff-0.1.1.tar.gz

Download URL dictsdiff-0.1.1.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8f810588de6bafdb2fc8b95dd40e45236a48c24fe8c868c8a3782a88d9056bea
BLAKE2b-256 checksum
How to use checksums
c2b174f09a68888f00a4da4a7507f67c1f8fe700f690b265dfd1e396cec6d8d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

Security for dictsdiff 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page