Code Monkey home page Code Monkey logo

Comments (1)

samLozier avatar samLozier commented on August 18, 2024

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)

from deepdiff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.