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

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

Open
samLozier opened this issue Jun 7, 2024 · 1 comment

Comments

@samLozier
Copy link

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
Copy link
Author

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)

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