比较两个JSON文件,将更改应用到另一个系统。

huangapple go评论77阅读模式
英文:

Compare 2 json files, apply that changes to another system

问题

Sure, here's the translated code portion:

我有2个JSON文件"original.json""changed.json"还有2个系统每个系统都有 "original.json"在我的其他代码中如果用户修改JSON文件那么将保存修改后的JSON文件为 "changed.json"所以我想比较 "original.json""changed.json" 以找出两个JSON文件之间的差异并将这些差异保存为 "result.json"
因此在其他系统中我希望将这些更改应用到它们的 "original.json"使其与其他系统的 "original.json" 相同

或者如果有更好的方法请告诉我

"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"
    }
  }
}

我尝试使用jsondiff来比较这两个JSON文件

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)

我不知道如何通过合并 "original.json""result.json" 来应用更改

Please note that the code above contains placeholders like original_data, changed_data, target_path, and datetime which need to be defined or imported appropriately in your actual code.

英文:

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"

答案1

得分: 1

import json
from deepdiff import DeepDiff
import jsonpatch

# 加载原始、当前和更改后的 JSON 文件
def load_json_file(filename):
    with open(filename, 'r') as file:
        data = json.load(file)
    return data

# 保存 JSON 文件
def save_json_file(filename, data):
    with open(filename, 'w') as file:
        json.dump(data, file, indent=2)

def compare_and_merge():
    # 加载 JSON 文件
    original = load_json_file("original.json")
    changed = load_json_file("changed.json")
    current = load_json_file("current.json")

    # 比较原始和更改后的 JSON 文件
    ddiff = DeepDiff(original, changed, ignore_order=True).to_dict()

    # 将差异保存到一个 JSON 文件
    save_json_file("differences.json", ddiff)

    # 生成补丁
    patch = jsonpatch.JsonPatch.from_diff(original, changed)

    # 应用补丁
    updated_current = patch.apply(current)

    # 将更新后的当前数据保存到文件
    save_json_file("current.json", updated_current)

# 运行这个函数
compare_and_merge()
英文:

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()

huangapple
  • 本文由 发表于 2023年6月15日 09:48:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定