Skip to main content

A recursive json comparison library that handles list orders and fuzzy types

Project description

A library to compare any json string/bytes/json-like-objects.

Version 2.0 is a rewrite to support more clear and easy-to-use functions(backward incompatible, so I started a new repo).

Version 1.20 is an enhance version that supports omit_path to ignore omitted keys in dict, and fixes many bugs, esp counting message inaccurate issue when same length & element collections; and wrong not_found info when different length under ignore_list_seq=True

Version 1.19 is an enhance version that fixes small bugs with strict_number_type supported to make 1 != 1.0.

Version 1.18 is a quickfix + enhance version that fixes custom handlers cannot be passed recursively bug, and float_fuzzy_digits can be passed directly.

Version 1.17 is an enhance version that supports custom handlers to handle outputs.

Version 1.16 is a quickfix version that supports Python 3.8 (and 3.9 as well).

Features

  • Compare jsons and print the differences (what and where they are, recursion supported). Useful for interface testing.

  • Config whether it will ignore the order of items in a list or not, recursively.

  • Both python 2.6-2.7 and 3.5-3.9 supported. (New)

  • Regular expressions supported for string to skip unconcerned keys or just to assert the format.

  • Compact str and unicode (or bytes and str in python3), they are considered equal. Good for non ascii coding languages.

  • Both json string (unicode or binary str) and json object (dict, list or tuple) are supported.

  • Support tuples, so results from pymysql.cursors.DictCursor can compare with interface response directly.

  • Json type legal check(strict_json).

  • Support skipping anywhere using argument like ignore_path=[“/a/1/k”, “/a/1/l”], dict keys or list indexes. Skipped fields are regarded as match.

  • The ignore_path list now support regular expressions too. You can use [r“^(/:raw-latex:`d+`/a)”] as ignore_path to skip all keys named “a” in [{“a”: 1, “b”: 2}, {“a”: 1, “b”: 4}] but still compare the value of “b”. (New)

    • Useful when compare multi records in database query result (dictionary cursor) with some fields unconcerned.

  • Fuzzy equal when handling floats.

  • Custom handlers supported.

  • Strict_number_type option to make int(1) != float(1.0) supported.

  • Emit keys in dict compare supported. (New)

QuickStart

install

pip install jsoncomparedeep

or update

pip install -U jsoncomparedeep

a simple example

from json_compare import compare
print(compare({"key1":["v1","v2"],"key2":{"key3":1}},{"key1":["v2","v1"],"key2":{"key3":2}}))

to see

a is {'key1': ['v1', 'v2'], 'key2': {'key3': 1}}
b is {'key1': ['v2', 'v1'], 'key2': {'key3': 2}}
ignore_list_seq = True, re_compare = True, ignore_path = None, omit_path = None, float_fuzzy_digits = 0
different value at /key2/key3
a: 1
b: 2
False

For more demos and information, just install it and visit the test file test_json_compare.py in Your_Python_Path/Lib/site-packages/json_compare/

Small Hints

  • Datetime in SQL result is not JSON serializable type, use something like CAST(create_time as CHAR) ‘create_time’ in SQL statement to solve it.

Bug report

  • Issues and bugs report to rainydew@qq.com.

  • Homepage icon leads to my Github project page, issues / PRs / stars are welcomed :)

Project details


Release history Release notifications | RSS feed

This version

2.0

Download files

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

Source Distribution

json-compare-deep-2.0.tar.gz (7.9 kB view details)

Uploaded Source

File details

Details for the file json-compare-deep-2.0.tar.gz.

File metadata

  • Download URL: json-compare-deep-2.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for json-compare-deep-2.0.tar.gz
Algorithm Hash digest
SHA256 9197b9de515c250f0b1f27fcf207e53c03ed3912f1d63e663ef9da0dc2091225
MD5 1fd8d48a66554fe8af053fbbbd1dfa56
BLAKE2b-256 80fa1d549d9204511c30064435c9fafa210b2e8678d83a2e4b11058f5c217cc0

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