deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Documentation on how to create an object only containing changed elements.

Open samLozier opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I'm trying to compare two json payloads. Essentially I want : diff_json = todays_json - yesterdays_json. It seems like this should be possible, but so far I haven't figured out how to do it. If it is, documentation would be helpful.

The end result should include the full original paths to the changed value, with all full-common keys and values removed.

eg:

# payload1 = {'a': [{'aa':'1'}, {'bb': '2'}]}
# payload2 = {'a': [{'aa':'2'}, {'bb': '2'}]}

# desired_output {'a': [{'aa': '2']}

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

samLozier avatar Jun 07 '24 21:06 samLozier

This seems to work, but I'm not sure it's the best solution.

def diff_json(json1, json2):
  differences = DeepDiff(json1, json2, ignore_order=True).to_dict()

  def extract_changes(diffs):
    result = {}
    for change_type in ['values_changed', 'type_changes', 'dictionary_item_added', 'dictionary_item_removed',
                        'iterable_item_added', 'iterable_item_removed']:
      if change_type in diffs:
        for key, change in diffs[change_type].items():
          path = key.lstrip("root")
          if change_type == 'values_changed' or change_type == 'type_changes':
            # For value changes or type changes, we show the new value
            path = path.replace('[', '.[')  # Adjust path for lists
            set_nested_value(result, path, change['new_value'])
          elif change_type in ['dictionary_item_added', 'iterable_item_added']:
            path = path.replace('[', '.[')  # Adjust path for lists
            set_nested_value(result, path, change['value'])
          # For removed items, we skip them as we are only interested in the new state
    return result

  def set_nested_value(dct, path, value):
    keys = re.findall(r'\w+|\[\d+\]', path)
    current = dct
    for key in keys[:-1]:
      if re.match(r'\[\d+\]', key):  # If the key is a list index
        index = int(key[1:-1])
        while len(current) <= index:
          current.append({})
        current = current[index]
      else:
        if key not in current:
          current[key] = {}
        current = current[key]
    final_key = keys[-1]
    if re.match(r'\[\d+\]', final_key):
      index = int(final_key[1:-1])
      while len(current) <= index:
        current.append({})
      current[index] = value
    else:
      current[final_key] = value

  return extract_changes(differences)

output = diff_json(payload2, payload1)

samLozier avatar Jun 07 '24 23:06 samLozier