-1

I have 2 json files. "original.json" and "changed.json", and also have 2 systems and each systems have "original.json". In my other codes, if user modify json file, then will save changed json files as "changed.json". So, I want compare "original.json" and "changed.json" to figure out diffences between 2 json files and save that differences as "result.json". So, in other systems, I want it to apply changes to its "original.json" to make same with other system's "original.json".

Or if there are any better way, please let me know.

"original.json"

{
  "ToDoList": {
    "Task1": {
      "Date": "20230613",
      "Name": "Daniel",
      "Description": "a"
    }
  },
  "Contacts": {
    "Contact_1": {
      "Name": "Daniel",
      "Office": "8F C03"
    },
    "Contact_2": {
      "Name": "Michael",
      "Office": "10F A12"
    }
  }
}

"changed.json"

{
  "ToDoList": {
    "Task1": {
      "Date": "20230613",
      "Name": "Daniel",
      "Description": "a"
    }
  },
  "Contacts": {
    "Contact_1": {
      "Name": "Daniel",
      "Office": "8F C03"
    }
  }
}

I tried to compare 2 json files by using jsondiff.

differences = jsondiff.diff(original_data, changed_data, dump=True)

result_json_name = f"result_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
result_path = os.path.join(target_path, result_json_name)
with open(result_path, 'w') as file:
    json.dump(differences, file)

And I don't have any idea to apply changes by merging "original.json" and "result.json"

  • 2
    Why not simply replacing `original` with `changed` as a whole? – Julien Jun 15 '23 at 02:09
  • Hi Julien, cause I just example for the working between 2 systems, but actually my systems have about 5~600 computers on it. So, I think there might be an error when some users change different parts at same time. So, I want just apply differences. – Achee_cute Jun 15 '23 at 04:15

1 Answers1

0

I just found correct way to patch json files with differences. Lovely Chat GPT :))

import json
from deepdiff import DeepDiff
import jsonpatch

# Load original, current and changed JSON files
def load_json_file(filename):
    with open(filename, 'r') as file:
        data = json.load(file)
    return data

# Save a JSON file
def save_json_file(filename, data):
    with open(filename, 'w') as file:
        json.dump(data, file, indent=2)

def compare_and_merge():
    # Load JSON files
    original = load_json_file("original.json")
    changed = load_json_file("changed.json")
    current = load_json_file("current.json")

    # Compare original and changed JSON files
    ddiff = DeepDiff(original, changed, ignore_order=True).to_dict()

    # Save the differences to a JSON file
    save_json_file("differences.json", ddiff)

    # Generate patch
    patch = jsonpatch.JsonPatch.from_diff(original, changed)

    # Apply patch
    updated_current = patch.apply(current)

    # Save the updated current to a file
    save_json_file("current.json", updated_current)

# Run the function
compare_and_merge()