Simple but powerfull JSON-files comparator
Project description
json-compare
Json-compare is a simple package that allows you to easily and fastly compare two .json files. Support key and multi-key comparison. You can also ignore certain fields' values or perform comparison insensitive to data types.
Usage
Compare files just as they are:
from json_compare import JSONComparator
comparator = JSONComparator(
left_file_path="expected.json",
right_file_path="actual.json",
)
# compare "actual.json" from the perspective of "expected.json"'s structure
comparator.compare_with_right() # / compare_with_left() / full_compare()
# save diff logs to ".comparison_logs" folder
comparator.save_diff_logs(path="comparison_logs")
# or print them into stdout
diffs = comparator.diff_log.log
print("\n".join(diffs))
# or print only summary. Here's an example:
print(comparator.diff_log.get_summary())
# OUTPUT example:
# ---------------------
# TOTAL: 4 differences
# -missing_obj_property: 3
# -unequal_value: 4
Set key property to perform more accurate comparisons of objects in arrays:
# expected.json: {"cats": [{"id": 4, "name": "Nyan"}, {"id": 2, "name": "Marx"}, {"id": 8, "name": "Flake"}]}
# actual.json: {"cats": [{"id": 2, "name": "Marx"}, {"id": 4, "name": "Naan"}]}
comparator = JSONComparator(
left_file_path="expected.json",
right_file_path="actual.json",
key="DATA//cats//<array>//id", # <----- just pass a "path" to needed property with following keywords:
) # DATA - points to the root of file
# <array> - indicates array with key property's object
In this case, saved diff log would look like that:
actual.json//cats//<array>
lack of items in array: expected 3 items, got only 2
actual.json//cats//<array>//[0]//name
unequal values: expected "Nyan", got "Naan" instead
actual.json//cats//<array>//[2]
missing array item: expected <object> with "id"=8
You can go further and add non-important fields to ignore parameter:
# expected.json: [{"id": 4, "name": "Nyan", "age": 2}, {"id": 2, "name": "Marx", "age": 7}, {"id": 8, "name": "Flake", "age": 4}]
# actual.json: [{"id": 2, "name": "Marx", "age": 7}, {"id": 4, "name": "Naan", "age": "two"}, {"id": 9, "name": "Lol", "age": 1}]
comparator = JSONComparator(
left_file_path="expected.json",
right_file_path="actual.json",
key="DATA//<array>//id",
ignore="DATA//<array>//age" # <-------
)
And here the result:
actual.json//<array>//[0]//name
unequal values: expected "Nyan", got "Naan" instead
actual.json//<array>//[2]
missing array item: expected <object> with "id"=8
If you want to compare ignoring type-differences between similar values
like "1.4" vs 1.4 or "[\"New Age №1\"]" vs ["New Age №1"] - just add ignore_types=True
param to JSONComparator:
comparator = JSONComparator(
left_file_path="expected.json",
right_file_path="actual.json",
key="DATA//<array>//id",
ignore_types=True, # <-------
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json-files-compare-1.0.1.tar.gz.
File metadata
- Download URL: json-files-compare-1.0.1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.9 Darwin/22.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
504a866565427c7920d7dbf506fa23e653390082460082fb5d7715312b43bdcc
|
|
| MD5 |
b79ea9731bd92990f5247d81ce2a443a
|
|
| BLAKE2b-256 |
2a215a36c1af33e20da33dcdf978fd73fb701aa553040a07d175299a102e739f
|
File details
Details for the file json_files_compare-1.0.1-py3-none-any.whl.
File metadata
- Download URL: json_files_compare-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.1 CPython/3.10.9 Darwin/22.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6a62155f203b0463b7a2fde8a036b7f0e61b3c16d07cb656ed2b140a4828225
|
|
| MD5 |
72efd29c3d2366b607020bfe32498563
|
|
| BLAKE2b-256 |
ec4486f461e7f533925d07871f50427a9150d9db2eac655ba3de9681c5a32858
|