I want to merge multiple json files into one file in python. The thing that I want to do is if there are several .json files like:
JavaScript
x
18
18
1
# temp1.json
2
[{'num':'1', 'item':'smartphone','data':'2019-01-01'},
3
{'num':'2', 'item':'smartphone','data':'2019-01-02'},
4
{'num':'3', 'item':'smartphone','data':'2019-01-03'},
5
{'num':'4', 'item':'smartphone','data':'2019-01-04'}]
6
7
# temp2.json
8
[{'num':'5', 'item':'smartphone','data':'2019-01-05'},
9
{'num':'6', 'item':'smartphone','data':'2019-01-06'},
10
{'num':'7', 'item':'smartphone','data':'2019-01-07'}]
11
12
# temp3.json
13
[{'num':'8', 'item':'smartphone','data':'2019-01-08'},
14
{'num':'9', 'item':'smartphone','data':'2019-01-09'},
15
{'num':'10', 'item':'smartphone','data':'2019-01-10'},
16
{'num':'11', 'item':'smartphone','data':'2019-01-11'},
17
{'num':'12', 'item':'smartphone','data':'2019-01-12'}]
18
The result.json files I want to get should look like:
JavaScript
1
14
14
1
# result.json
2
[{'num':'1', 'item':'smartphone','data':'2019-01-01'},
3
{'num':'2', 'item':'smartphone','data':'2019-01-02'},
4
{'num':'3', 'item':'smartphone','data':'2019-01-03'},
5
{'num':'4', 'item':'smartphone','data':'2019-01-04'},
6
{'num':'5', 'item':'smartphone','data':'2019-01-05'},
7
{'num':'6', 'item':'smartphone','data':'2019-01-06'},
8
{'num':'7', 'item':'smartphone','data':'2019-01-07'},
9
{'num':'8', 'item':'smartphone','data':'2019-01-08'},
10
{'num':'9', 'item':'smartphone','data':'2019-01-09'},
11
{'num':'10', 'item':'smartphone','data':'2019-01-10'},
12
{'num':'11', 'item':'smartphone','data':'2019-01-11'},
13
{'num':'12', 'item':'smartphone','data':'2019-01-12'}]
14
The result.json files I got is:
JavaScript
1
14
14
1
# result.json
2
[[{'num':'1', 'item':'smartphone','data':'2019-01-01'},
3
{'num':'2', 'item':'smartphone','data':'2019-01-02'},
4
{'num':'3', 'item':'smartphone','data':'2019-01-03'},
5
{'num':'4', 'item':'smartphone','data':'2019-01-04'}],
6
[{'num':'5', 'item':'smartphone','data':'2019-01-05'},
7
{'num':'6', 'item':'smartphone','data':'2019-01-06'},
8
{'num':'7', 'item':'smartphone','data':'2019-01-07'}],
9
[{'num':'8', 'item':'smartphone','data':'2019-01-08'},
10
{'num':'9', 'item':'smartphone','data':'2019-01-09'},
11
{'num':'10', 'item':'smartphone','data':'2019-01-10'},
12
{'num':'11', 'item':'smartphone','data':'2019-01-11'},
13
{'num':'12', 'item':'smartphone','data':'2019-01-12'}]]
14
I used the code to merge .json files from here and changed it very slightly like below:
JavaScript
1
13
13
1
files=['my.json','files.json', ,'name.json']
2
3
def merge_JsonFiles(filename):
4
result = list()
5
for f1 in filename:
6
with open(f1, 'r') as infile:
7
result.append(json.load(infile))
8
9
with open('counseling3.json', 'w') as output_file:
10
json.dump(result, output_file)
11
12
merge_JsonFiles(files)
13
I already read several related questions, but there is no answer I need. Can anyone help me?
Advertisement
Answer
You should use extend
instead of append
. It will add the items of the passed list to result
instead of a new list:
JavaScript
1
13
13
1
files=['my.json','files.json', ,'name.json']
2
3
def merge_JsonFiles(filename):
4
result = list()
5
for f1 in filename:
6
with open(f1, 'r') as infile:
7
result.extend(json.load(infile))
8
9
with open('counseling3.json', 'w') as output_file:
10
json.dump(result, output_file)
11
12
merge_JsonFiles(files)
13