Skip to main content

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.

made-with-python

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,  # <-------
)  

Metadata

Release files for json-files-compare 1.0.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 json-files-compare 1.0.1
File Size Uploaded Provenance Actions
json-files-compare-1.0.1.tar.gz 6.7 kB Without provenance Details for json-files-compare-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for json-files-compare 1.0.1
File Interpreter ABI Platform Actions
json_files_compare-1.0.1-py3-none-any.whl Python 3 none any Details for json_files_compare-1.0.1-py3-none-any.whl

Total release size: 13.9 kB

Release files / json-files-compare-1.0.1.tar.gz

Download URL json-files-compare-1.0.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
504a866565427c7920d7dbf506fa23e653390082460082fb5d7715312b43bdcc
BLAKE2b-256 checksum
How to use checksums
2a215a36c1af33e20da33dcdf978fd73fb701aa553040a07d175299a102e739f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.10.9 Darwin/22.3.0

Release files / json_files_compare-1.0.1-py3-none-any.whl

Download URL json_files_compare-1.0.1-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d6a62155f203b0463b7a2fde8a036b7f0e61b3c16d07cb656ed2b140a4828225
BLAKE2b-256 checksum
How to use checksums
ec4486f461e7f533925d07871f50427a9150d9db2eac655ba3de9681c5a32858
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.10.9 Darwin/22.3.0

Security for json-files-compare 1.0.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

1.0.1 This release

2 release files 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