0

How to merge two json files in one with all info from 1 file and second file?

For example:

json1 = {"info": [{"id": "3", "book_id": "88" }]}
json2 = {"info": [{"id": "100", "book_id": "77" }]}
final_result = {
  "info": [
    {"id": "3", "book_id": "88" },
    {"id": "100", "book_id": "77"}
  ]
}

Now it's just update it, and remove all info from first folder

import json

with open("folder1/1.json") as fin1:
    data1 = json.load(fin1)
with open("folder2/1.json") as fin2:
    data2 = json.load(fin2)
data1.update(data2)
with open("together.json", "w") as fin3:
    json.dump(data1, fin3)
ForceBru
  • 43,482
  • 10
  • 63
  • 98
alexuuu
  • 53
  • 5
  • What does it _mean_ to "merge JSON files into one"? What is the result supposed to be? A list of objects? Or maybe new keys should be added? – ForceBru Nov 13 '20 at 14:03
  • results will be dict. just need to add them together – alexuuu Nov 13 '20 at 14:05
  • 1
    What does it _mean_ to "add them together"? Say, should `{1: 5} add {1: 6, 2: 9}` be `{1: 6, 2: 9}`, or `{1: [5, 6], 2: 9}`, or `{'1_old': 5, '1_new': 6, 2: 9}`, or...? In other words: an example would help understand how you want them to be "added". – ForceBru Nov 13 '20 at 14:07
  • json1 - `{"info": [{"id": "3", "book_id": "88" }]}`, json2 `{"info": [{"id": "100", "book_id": "77" }]}` = final_result `{"info": [{"id": "3", "book_id": "88" }, {"id": "100", "book_id": "77"]}` – alexuuu Nov 13 '20 at 14:11

2 Answers2

0

I'm guessing you have 2 objects with the same (or overlapping) keys. To write both to a new file you could do something like

json.dump([data1, data2], fin3)

or

json.dump({folder1: data1, folder2: data2}, fin3)

EDIT - given the structure of your expected output you specify, you can merge the objects like this

def merge_dicts(*dicts):
    r = {}
    for d in dicts:
        for k, v in d.items():
            r.setdefault(k, []).extend(v)
    return r

(or use one of the other solutions here or here)

Then you simply write this merged dictionary to json:

with open('data.json', 'w') as f:
    json.dump(merge_dicts(data1, data2), fin3)

In general remember data loaded from json is just a dictionary or list, so you can merge and manipulate them in the same way as other Python variables before saving them back to json.

Stuart
  • 9,597
  • 1
  • 21
  • 30
  • Hey ! Thanks you , can you tell me how to do it with all files in folder1 and folder2 (names of Jsons are same ). – alexuuu Nov 13 '20 at 15:01
0

It depends entirely on the type of data you have, suppose you want to merge 2 JSON files to generate a new one, where each JSON file is structured as part of a list you could do:

import json

with open("1.json") as fin1:
    data1 = json.load(fin1)
with open("2.json") as fin2:
    data2 = json.load(fin2)

new_json = [data1,data2]

with open('data.json', 'w') as f:
    json.dump(new_json, f)

Or you could do the same with dict syntax, both would generate a perfectly fine JSON. If you need to combine the two JSON files in a complex merge operation it becomes more complicated. A look on your data would help.

Felix Z.
  • 317
  • 2
  • 12