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 jsoncomparisonVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
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.
pip install jsoncomparison
diff = Compare().check(expected, actual)
from jsoncomparison import Compare, NO_DIFF
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.
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.