I have multiple json files containing relational data which I need to merge , each of the file has a record with commonkey which is common key in all files, in the example below a0 ,a1 are common keys.The value is a nested dictionary of multiple keys like Key1, key2 etc as shown below, I need to merge the multiple json files and get the output as shown in the dboutput.json, with the file name acting as the index in the merging operation. Such question is a related question which merges loosing information, but in my case I dont want any updation which replaces the existing keys or skips updations, in case of hitting an existing key another nested dictionary indexed by the filename is created as shown below:
Example:
File db1.json:
"a0": { "commonkey": [ "a1", "parentkeyvalue1" ], "key1": "kvalue1", "key2": "kvalue2" "keyp": "kvalue2abc" }, "a1": { ... }
File db2.json:
"a0": { "commonkey": [ "a1", "parentkeyvalue1" ], "key1": "kvalue1xyz", "key2": "kvalue2", "key3": "kvalue2" }, "a1": { ... }
Desired Output
File dboutput.json
"a0": { "commonkey": [ "a1", "parentkeyvalue1" ], "key1": {"db1":"kvalue1","db2":"kvalue1xyz"} , "key2": {"db1":"kvalue2","db2":"kvalue2"} , "key3": {"db2":"kvalue2"} "keyp": {"db1":"kvalue2abc"} }, "a1": { ... }
So how to do such lossless merges? Note "key2": {"db1":"kvalue2","db2":"kvalue2"} even if the key\value pairs are same they need to be stored separately. In effect the output is a union of all input files and has all entries from all other files.
Also
"commonkey": [
"a1",
"parentkeyvalue1"
],
will be same for all files and hence need not be repeated