如何在python中将多个json文件合并为一个文件

时间:2019-08-09 02:13:51

标签: python json merge

我想将多个json文件合并为python中的一个文件。我想做的事情是是否有几个.json文件,例如:

# temp1.json
[{'num':'1', 'item':'smartphone','data':'2019-01-01'},
{'num':'2', 'item':'smartphone','data':'2019-01-02'},
{'num':'3', 'item':'smartphone','data':'2019-01-03'},
{'num':'4', 'item':'smartphone','data':'2019-01-04'}]

# temp2.json
[{'num':'5', 'item':'smartphone','data':'2019-01-05'},
{'num':'6', 'item':'smartphone','data':'2019-01-06'},
{'num':'7', 'item':'smartphone','data':'2019-01-07'}]

# temp3.json
[{'num':'8', 'item':'smartphone','data':'2019-01-08'},
{'num':'9', 'item':'smartphone','data':'2019-01-09'},
{'num':'10', 'item':'smartphone','data':'2019-01-10'},
{'num':'11', 'item':'smartphone','data':'2019-01-11'},
{'num':'12', 'item':'smartphone','data':'2019-01-12'}]

我要获取的result.json文件应如下所示:

# result.json
[{'num':'1', 'item':'smartphone','data':'2019-01-01'},
{'num':'2', 'item':'smartphone','data':'2019-01-02'},
{'num':'3', 'item':'smartphone','data':'2019-01-03'},
{'num':'4', 'item':'smartphone','data':'2019-01-04'},
{'num':'5', 'item':'smartphone','data':'2019-01-05'},
{'num':'6', 'item':'smartphone','data':'2019-01-06'},
{'num':'7', 'item':'smartphone','data':'2019-01-07'},
{'num':'8', 'item':'smartphone','data':'2019-01-08'},
{'num':'9', 'item':'smartphone','data':'2019-01-09'},
{'num':'10', 'item':'smartphone','data':'2019-01-10'},
{'num':'11', 'item':'smartphone','data':'2019-01-11'},
{'num':'12', 'item':'smartphone','data':'2019-01-12'}]

我得到的result.json文件是:

# result.json
[[{'num':'1', 'item':'smartphone','data':'2019-01-01'},
{'num':'2', 'item':'smartphone','data':'2019-01-02'},
{'num':'3', 'item':'smartphone','data':'2019-01-03'},
{'num':'4', 'item':'smartphone','data':'2019-01-04'}],
[{'num':'5', 'item':'smartphone','data':'2019-01-05'},
{'num':'6', 'item':'smartphone','data':'2019-01-06'},
{'num':'7', 'item':'smartphone','data':'2019-01-07'}],
[{'num':'8', 'item':'smartphone','data':'2019-01-08'},
{'num':'9', 'item':'smartphone','data':'2019-01-09'},
{'num':'10', 'item':'smartphone','data':'2019-01-10'},
{'num':'11', 'item':'smartphone','data':'2019-01-11'},
{'num':'12', 'item':'smartphone','data':'2019-01-12'}]]

我使用该代码合并了here中的.json文件,并进行了如下更改:

files=['my.json','files.json',...,'name.json']

def merge_JsonFiles(filename):
    result = list()
    for f1 in filename:
        with open(f1, 'r') as infile:
            result.append(json.load(infile))

    with open('counseling3.json', 'w') as output_file:
        json.dump(result, output_file)

merge_JsonFiles(files)

我已经阅读了几个相关问题,但是没有答案。谁能帮我吗?

3 个答案:

答案 0 :(得分:1)

import json
import pandas as pd

with open('example1.json') as f1:               # open the file
    data1 = json.load(f1)

with open('example2.json') as f2:                # open the file       
    data2 = json.load(f2)
    
df1 = pd.DataFrame([data1])                      # Creating DataFrames
df2 = pd.DataFrame([data2])                      # Creating DataFrames

MergeJson = pd.concat([df1, df2], axis=1)         # Concat DataFrames

MergeJson.to_json("MergeJsonDemo.json")          # Writing Json

答案 1 :(得分:0)

您应该使用extend而不是append。它将传递的列表项添加到result而不是新列表中:

files=['my.json','files.json',...,'name.json']

def merge_JsonFiles(filename):
    result = list()
    for f1 in filename:
        with open(f1, 'r') as infile:
            result.extend(json.load(infile))

    with open('counseling3.json', 'w') as output_file:
        json.dump(result, output_file)

merge_JsonFiles(files)

答案 2 :(得分:0)

还有另一种方法,只需将这些文件中的json文本作为python列表加载,然后将它们添加在一起即可。代码如下。

# temp1.json
json_a = [{'num':'1', 'item':'smartphone','data':'2019-01-01'},
{'num':'2', 'item':'smartphone','data':'2019-01-02'},
{'num':'3', 'item':'smartphone','data':'2019-01-03'},
{'num':'4', 'item':'smartphone','data':'2019-01-04'}]

# temp2.json
json_b = [{'num':'5', 'item':'smartphone','data':'2019-01-05'},
{'num':'6', 'item':'smartphone','data':'2019-01-06'},
{'num':'7', 'item':'smartphone','data':'2019-01-07'}]

# temp3.json
json_c = [{'num':'8', 'item':'smartphone','data':'2019-01-08'},
{'num':'9', 'item':'smartphone','data':'2019-01-09'},
{'num':'10', 'item':'smartphone','data':'2019-01-10'},
{'num':'11', 'item':'smartphone','data':'2019-01-11'},
{'num':'12', 'item':'smartphone','data':'2019-01-12'}]

print(json_a + json_b + json_c)

输出:

[{'num': '1', 'item': 'smartphone', 'data': '2019-01-01'},
 {'num': '2', 'item': 'smartphone', 'data': '2019-01-02'},
 {'num': '3', 'item': 'smartphone', 'data': '2019-01-03'},
 {'num': '4', 'item': 'smartphone', 'data': '2019-01-04'},
 {'num': '5', 'item': 'smartphone', 'data': '2019-01-05'},
 {'num': '6', 'item': 'smartphone', 'data': '2019-01-06'},
 {'num': '7', 'item': 'smartphone', 'data': '2019-01-07'},
 {'num': '8', 'item': 'smartphone', 'data': '2019-01-08'},
 {'num': '9', 'item': 'smartphone', 'data': '2019-01-09'},
 {'num': '10', 'item': 'smartphone', 'data': '2019-01-10'},
 {'num': '11', 'item': 'smartphone', 'data': '2019-01-11'},
 {'num': '12', 'item': 'smartphone', 'data': '2019-01-12'}]