Skip to main content

A simple library to create a minimal diff of two json compatible dictionaries

Project description

Json Dict Diff

Library for creating a minimal diff of two json-compatible Python dictionaries.

This library can be used to compare two json-compatible Python dictionaries (i.e. dictionaries, lists, ... containing only the following primitives: int, float, str, list, dict, set, tuple, bool) and generates a "minimal" diff. The diff is represented as a dictionary where insertions/deletions/substitutions are represented as tuples. For example the following two dictionaries:

diff(
  { "a": [ { "b": 1, "c": True }, 1.0, "x" ], "d": 1 },
  { "a": [ { "b": 2, "c": True }, 1.0, "y", True ] }
)

will result in the following diff:

{ "a": [ { "b": (1, 2) }, ("x", "y"), (None, True) ], "d": (1, None) }

Here the tuples (1, 2), ("x", "y"), (None, True), (1, None) represent the substitions that happened. None implies that an object was either removed or added.

If there is no diff (e.g.: diff({ "a": 1 }, { "a": 1 })) the result will be None. In this sense, the library can also be used to compare two json-compatible dictionaries.


If an object of any other type is used in a dict, list, ..., a ValidationException will be raised.

Exports

JDict = Union[int, float, str, List["Result"], Dict[str, "Result"], Tuple["Result"], Set["Result"], None]

class ValidationException

def validate(a: JDict):

def diff(a: JDict, b: JDict) -> JDict:

Notes

Primitive objects such as ints, floats, ... can also be diffed which will result in a single tuple of a substition. For example:

diff("a", "b") == ("a", "b")
diff("a", "a") is None

The diff will treat lists, sets and tuples as lists, which means that the result will contain lists instead of sets or tuples. This is due to tuples being reserved for the substitution results.


If it is not clear which element in a list might be substituted this library makes no guarantees on the actual substitution. For example:

diff([1, 2, 3], [1, 4, 5])

can result either in: [ (2, 4), (3, 5) ] or [ (3, 4), (2, 5) ].


None elements might or might not show up as a diff. For example:

diff([ None, 1 ], [1])

might result in [ (None, 1) ] or None (i.e. no diff).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_dict_diff-1.0.5.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

json_dict_diff-1.0.5-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file json_dict_diff-1.0.5.tar.gz.

File metadata

  • Download URL: json_dict_diff-1.0.5.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for json_dict_diff-1.0.5.tar.gz
Algorithm Hash digest
SHA256 44ceac744ba7b2274c138dabd8ba820d03893fc7607d6d4ef2a70df7107ae850
MD5 0b7e1a27d53128269a03a3f825790483
BLAKE2b-256 bd96b328916836f52b758e684930e8fccd92c764d46382c132f0ac29129bbfab

See more details on using hashes here.

File details

Details for the file json_dict_diff-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for json_dict_diff-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6d80fb1d236d1f46b8d5952f03871ca87062826393130d3c5cace0d523bdb416
MD5 083484e7c6f39a8651ca512a3861f0d6
BLAKE2b-256 06dad86ff8927dd2df05b89ab5e4fd58e968584702cd5cb2ad176147ae8491a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page