如何使用 json diff 和合并合并合并 json 子对象?

How to union-merge json child objects using json diff and merge?

我有两个 json 文件:

json1

     {
    "plans": {
      "buildings": [
        {
          "floors": ["1" "2" "3" ]
        },
        {
          "floors": ["1"]
        }
      ]
  }
}

json 2

 { "plans": {
          "buildings": [
            {
              "floors": ["3" "5" "6" ]
            },
            {
              "floors": []
            }
          ]
      }
    }

合并并集后需要 json

 {
    "plans": {
      "buildings": [
        {
          "floors": ["1","2","3","5","6" ]
        },
        {
          "floors": ["1"]
        }
      ]
  }
}

如何使用可用工具获取 json 差异和合并补丁来实现此操作。我不想遍历每个子节点并手动检查。

我也有和你一样的问题。我的解决方案是使用 JSON 补丁从 diffed JSON 制作补丁,然后发送补丁到另一个系统。在 http://jsonpatch.com/ 寻找更多信息。他们提供包含每种编程语言的库的存储库。

我为这些场景的差异和合并创建了一个灯光 api。开源:https://github.com/ryanshane/JsonDiffMerge

对于数组,它们没有主键,因此除非您将字符串数组更改为对象,否则您最终会得到重复的条目,例如 [{ id: "uid string", value: "3" } ] 而不是 ["3"].