Search code examples
jsonmergejson-patch

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


I have two json files as such:

json1

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

json 2

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

Required json after merge union

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

How to achieve this operation by getting json diff and merge patching using available tools.I dont want to loop through each and every child nodes and check manually.


Solution

  • I have similar problem same as you too. My solution is use JSON patch to make patch from diffed JSON then send patch to apply at another system. Look for more information at http://jsonpatch.com/. They provide repository that have library for each programming language.