Merge json files by key python
Ngày đăng:
17/09/2022
Trả lời:
0
Lượt xem:
124
I am trying to combine multiple JSON files in a Ubuntu platform. As an example, the data from two files are as follows: File_1
File_2
I wrote a Python script to combine them. I added a new line and a comma after each record.
The output from the merge file is:
When I validate these records using JSON Lint (http://jsonlint.com/), it tells me that the file is broken and not a valid JSON. even after spending quite sometime, I am not able to figure out what is going wrong with the merging. It would be helpful is anyone has any thoughts on this. How do I merge two JSON files in Python?I am using the below code to merge the files:. data = []. for f in glob.glob("*.json"):. with open(f,) as infile:. data.append(json.load(infile)). with open("merged_file.json",'w') as outfile:. json.dump(data, outfile). out: [[[a,b],[c,d],[e,f]],[[g,h],[i,f],[k,l]],[[m,n],[o,p],[q,r]]]. Can we merge two JSON files?JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java. util. Map) in the below program.
What is JSON dump Python?The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.
|