Registry / testing / jsoncomparison

jsoncomparison

JSON →
library1.1.0pypypi✓ verified 24d ago

The `jsoncomparison` package is a Python utility designed to compare two objects with a JSON-like structure and data types. It provides a flexible way to identify differences between expected and actual JSON objects, making it suitable for tasks like API testing and configuration validation. The library is currently active, with its latest stable version being 1.1.0.

pip install jsoncomparison
INSTALL
IMPORT
SIG · JSONCOMPARISON
J
jsoncomparison
testingpythonv1.1.0
Install
1.6s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.010s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.010s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Compare
from jsoncomparison import Compare
The primary class for performing JSON comparisons.
NO_DIFF
from jsoncomparison import NO_DIFF
A constant used to check if no differences were found after comparison.

This quickstart demonstrates how to use `jsoncomparison.Compare` to check for differences between two JSON-like Python objects (dictionaries). It also includes an example of using `ignore_rules` to skip specific keys during comparison, which is useful for dynamic fields like timestamps. The `check` method returns a dictionary detailing the differences or `NO_DIFF` if the objects are identical based on the comparison rules.

from jsoncomparison import Compare, NO_DIFF expected = { "project": { "name": "jsoncomparison", "version": "0.1", "license": "MIT", "language": { "name": "python", "versions": [3.5, 3.6] } }, "os": "linux" } actual = { "project": { "name": "jsoncomparison", "version": 0.1, # Intentional type mismatch for demonstration "license": "Apache 2.0", "language": { "name": "python", "versions": [3.6] } } } # Perform comparison diff = Compare().check(expected, actual) # Check if differences exist if diff != NO_DIFF: print("Differences found:") print(diff) else: print("No differences found.") # Example with ignore rules for dynamic fields or irrelevant keys expected_with_dynamic = {"id": 1, "timestamp": "2023-01-01T12:00:00Z", "data": {"value": 10}} actual_with_dynamic = {"id": 1, "timestamp": "2023-01-02T10:30:00Z", "data": {"value": 10}} # Ignore 'timestamp' field compare_with_ignore = Compare(ignore_rules={'timestamp'}).check(expected_with_dynamic, actual_with_dynamic) if compare_with_ignore == NO_DIFF: print("\nNo differences found after ignoring 'timestamp'.") else: print("\nDifferences found even after ignoring 'timestamp':") print(compare_with_ignore)
Debug
Known issues
gotchaBy default, `jsoncomparison` checks for both value and type equality. A common footgun is comparing a numeric value (e.g., `1`) in one JSON object with a string representation of the same number (e.g., `'1'`) in another. This will be flagged as a type mismatch, even if the values appear numerically equivalent.
fix
Ensure that corresponding values in your `expected` and `actual` JSON objects have consistent data types, or implement custom comparison logic using the library's extension points if flexible type matching is required.
affects: All versions
gotchaThe library's comparison is order-sensitive for arrays by default. If the order of elements within an array does not matter for your use case, `jsoncomparison` will report differences if the order varies. This is a common pitfall in JSON comparison generally.
fix
Utilize the `ignore_order` parameter in the `Compare` constructor or `ignore_rules` to specify paths where array order should be disregarded. Refer to the official documentation for advanced configuration of ignore rules.
affects: All versions
gotchaWhen comparing JSON objects that contain dynamic fields (e.g., timestamps, auto-generated IDs, or fields whose values are expected to change frequently but are not critical for comparison), these will always be reported as differences. Failing to account for them can lead to flaky tests or cluttered difference reports.
fix
Use the `ignore_rules` parameter in the `Compare` constructor to specify keys or paths that should be excluded from the comparison. This helps focus the comparison on relevant data.
affects: All versions
gotchaBy default, `jsoncomparison`'s configuration typically does not print the comparison result directly to the console; instead, it writes results to a file. Users expecting immediate console output during debugging might miss this default behavior.
fix
To view comparison results directly in the console, check the library's documentation for configuration options to enable console output, or manually read and print the generated diff file.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'jsoncomparison'
The 'jsoncomparison' package is not installed in your Python environment or the environment where your script is being run.
fix
pip install jsoncomparison
TypeError: 'Compare' object is not callable
You are trying to call the 'Compare' class instance as if it were a function, or you forgot to instantiate it.
fix
diff = Compare().check(expected, actual)
AttributeError: module 'jsoncomparison' has no attribute 'Compare'
You are trying to access 'Compare' directly from the 'jsoncomparison' module without importing it explicitly, or you're using an incorrect import statement.
fix
from jsoncomparison import Compare, NO_DIFF
{'_message': 'Types not equal. Expected: <type1>, received: <type2>'}
The 'jsoncomparison' library found a difference in the data types of corresponding values in the 'expected' and 'actual' JSON objects.
fix
Ensure that the data types of corresponding values in your 'expected' and 'actual' JSON objects match, or use custom comparison rules if type differences are acceptable for specific fields.
{'_message': 'Key does not exists.'}
The 'jsoncomparison' library detected that a key present in the 'expected' JSON object is missing from the 'actual' JSON object.
fix
Verify that all expected keys are present in the 'actual' JSON, or define ignore rules using the `ignore_rules` parameter in the `Compare` constructor if missing keys are acceptable for certain comparisons.
Upgrade
Version history
1.1.0latest on PyPI · released May 17, 2021
Audit
Dependencies
pythonrequiredRequired Python version for execution.
Agent activity
3 hits · last 30 days
node
2
Resources
jsoncomparison — pip install jsoncomparison · libregistry