{"id":2083,"library":"jsoncomparison","title":"JSON Comparison","description":"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.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/rugleb/JsonCompare","tags":["json","comparison","diff","testing","validation"],"install":[{"cmd":"pip install jsoncomparison","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required Python version for execution.","package":"python","optional":false}],"imports":[{"note":"The primary class for performing JSON comparisons.","symbol":"Compare","correct":"from jsoncomparison import Compare"},{"note":"A constant used to check if no differences were found after comparison.","symbol":"NO_DIFF","correct":"from jsoncomparison import NO_DIFF"}],"quickstart":{"code":"from jsoncomparison import Compare, NO_DIFF\n\nexpected = {\n    \"project\": {\n        \"name\": \"jsoncomparison\",\n        \"version\": \"0.1\",\n        \"license\": \"MIT\",\n        \"language\": {\n            \"name\": \"python\",\n            \"versions\": [3.5, 3.6]\n        }\n    },\n    \"os\": \"linux\"\n}\n\nactual = {\n    \"project\": {\n        \"name\": \"jsoncomparison\",\n        \"version\": 0.1,  # Intentional type mismatch for demonstration\n        \"license\": \"Apache 2.0\",\n        \"language\": {\n            \"name\": \"python\",\n            \"versions\": [3.6]\n        }\n    }\n}\n\n# Perform comparison\ndiff = Compare().check(expected, actual)\n\n# Check if differences exist\nif diff != NO_DIFF:\n    print(\"Differences found:\")\n    print(diff)\nelse:\n    print(\"No differences found.\")\n\n# Example with ignore rules for dynamic fields or irrelevant keys\nexpected_with_dynamic = {\"id\": 1, \"timestamp\": \"2023-01-01T12:00:00Z\", \"data\": {\"value\": 10}}\nactual_with_dynamic = {\"id\": 1, \"timestamp\": \"2023-01-02T10:30:00Z\", \"data\": {\"value\": 10}}\n\n# Ignore 'timestamp' field\ncompare_with_ignore = Compare(ignore_rules={'timestamp'}).check(expected_with_dynamic, actual_with_dynamic)\n\nif compare_with_ignore == NO_DIFF:\n    print(\"\\nNo differences found after ignoring 'timestamp'.\")\nelse:\n    print(\"\\nDifferences found even after ignoring 'timestamp':\")\n    print(compare_with_ignore)","lang":"python","description":"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."},"warnings":[{"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.","message":"By 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"By 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}