Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major bug when an iterable has both a change in an existing item AND adding/removing an additional item in the same key #520

Open
jbones001 opened this issue Feb 20, 2025 · 0 comments

Comments

@jbones001
Copy link

jbones001 commented Feb 20, 2025

Describe the bug
Major bug when an iterable has both a change in an existing item AND adding/removing an additional item in the same key. The changed key would only show "root['root_key'][0]" rather than the nested key name "root['root_key'][0]['name']" and then therefore shows the WHOLE node as a change rather than individual keys that have changed. This is a major issue as it makes it difficult to identify what has changed in the nested key.

To Reproduce

import json
from deepdiff import DeepDiff 

json1 ={
  "id": 1234,
  "name": "Rainbow Inc",
  "employees": [
    {
      "name": "John Doe",
      "role": "ceo"
    }
  ],
}

json2 ={
  "id": 1234,
  "name": "Rainbow Inc",
  "employees": [
    {
      "name": "Johnny Doe",
      "role": "ceo"
    },
    {
      "name": "James Doe",
      "role": "cfo"
    }
  ],
 
}

def checkForDifferences():
    diff = DeepDiff(json2, json1, ignore_order=True, ignore_string_case=True, verbose_level=2)  

    # print("\n###diff=",diff)
    # print("\n####diff.items()=",diff.items())

    # Get added keys
    added_keys = list(diff.get('dictionary_item_added', {}).keys())  #keys only
    added_keys2 = diff.get('dictionary_item_added', [])  #keys and values
    added_iterable_keys = list(diff.get('iterable_item_added', {}).keys())  #keys only
    added_iterable_keys2 = diff.get('iterable_item_added', [])  #keys and values
    
    # Get removed keys
    removed_keys = list(diff.get('dictionary_item_removed', {}).keys())  #keys only
    removed_keys2 = diff.get('dictionary_item_removed', [])  #keys and values
    removed_iterable_keys = list(diff.get('iterable_item_removed', {}).keys())  #keys only
    removed_iterable_keys2 = diff.get('iterable_item_removed', [])  #keys and values

    # Get changed keys
    changed_keys = list(diff.get('values_changed', {}).keys())  #keys only
    changed_keys2 = diff.get('values_changed', [])  #keys and values

  
    print("\n###################################")
    print("added_keys=",json.dumps(added_keys, indent=4))
    print("added_keys2=",json.dumps(added_keys2, indent=4)) #keys with values
    print("added_iterable_keys=",json.dumps(added_iterable_keys, indent=4))
    print("added_iterable_keys2=",json.dumps(added_iterable_keys2, indent=4)) #keys with values
    print("removed_keys=",json.dumps(removed_keys, indent=4))
    print("removed_keys2=",json.dumps(removed_keys2, indent=4)) #keys with values
    print("removed_iterable_keys=",json.dumps(removed_iterable_keys, indent=4))
    print("removed_iterable_keys2=",json.dumps(removed_iterable_keys2, indent=4)) #keys with values
    print("changed_keys=",json.dumps(changed_keys, indent=4)) #keys only
    print("changed_keys2=",json.dumps(changed_keys2, indent=4)) #keys with values


checkForDifferences()
# Current Output:
###################################
added_keys= []
added_keys2= []
added_iterable_keys= []
added_iterable_keys2= []
removed_keys= []
removed_keys2= []
removed_iterable_keys= [
    "root['employees'][1]"
]
removed_iterable_keys2= {
    "root['employees'][1]": {
        "name": "James Doe",
        "role": "cfo"
    }
}
changed_keys= [
]
changed_keys2= {
    **"root['employees'][0]"**: {
        "new_value": {
            "name": "John Doe",
            "role": "ceo"
        },
        "old_value": {
            "name": "Johnny Doe",
            "role": "ceo"
        }
    }
}



# Expected Output:
###################################
added_keys= []
added_keys2= []
added_iterable_keys= []
added_iterable_keys2= []
removed_keys= []
removed_keys2= []
removed_iterable_keys= [
    "root['employees'][1]"
]
removed_iterable_keys2= {
    "root['employees'][1]": {
        "name": "James Doe",
        "role": "cfo"
    }
}
changed_keys= [
    "root['employees'][0]['name']"
]
changed_keys2= {
    **"root['employees'][0]['name']"**: {
        "new_value": "john doe",
        "old_value": "johnny doe"
    }
}

Expected behavior
The expected output should show the nested key "root['employees'][0]['name']" has changed rather than the whole node "root['employees'][0]".

OS, DeepDiff version and Python version (please complete the following information):

  • OS: Windows
  • Version 10
  • Python Version 3.13
  • DeepDiff Version 8.2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant