Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I formulate ExcludePath regex for Python DeepDiff in this JSON?

I want to exclude Key7 and Key8 from the DeepDiff comparison. Even if the values of key7 and key 8 are different, I don't want DeepDiff to consider it.

diff = DeepDiff(object1, object2, excludePath)

The sample object looks like below:

Obj1:

{
    "KEY1": "VALUE1",
    "KEY2": "VALUE2",
    "KEY3": "VALUE3",
    "KEY4": 4,
    "KEY5": [
        {
            "KEY6": "VALUE",
            "KEY7": 6,
            "KEY8": VALUE
        }
    ]
}
like image 525
sachJuve Avatar asked Dec 12 '25 02:12

sachJuve


1 Answers

You can use exclude_regex_paths and pass a regex or a set or list of path regexes to exclude.
In your case:

>>> DeepDiff(object1, object2, exclude_regex_paths=r"\['KEY[78]'\]")

More details on exclude-regex-paths in the documentation

like image 189
Antiez Avatar answered Dec 14 '25 15:12

Antiez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!