合并多个JSON文件(两个以上)

时间:2018-10-18 17:07:12

标签: json python-3.x list dictionary merge

我想将多个JSON文件合并为一个文件。所有这些文件都具有相同的结构。例如,我创建了三个看起来像这样的文件:

ExampleFile_1

    {
      "items": [
        {
          "answers": [
            {
              "creation_date": 1538172165
            },
            {
              "creation_date": 1538172205
            },
            {
              "creation_date": 1538172245
            }
         ],
       "creation_date": 1538172012,
       "question_id": 52563137
       }
      ]
    }

ExampleFile_2

    {
      "items": [
        {
          "answers": [
            {
              "creation_date": 1538326991
            }
          ],
        "creation_date": 1538172095,
        "question_id": 52563147
        },
        {
          "answers": [
            {
              "creation_date": 1538180453
            }
          ],
        "creation_date": 1538172112,
        "question_id": 52563150
        }
      ]
    }

ExampleFile_3

   {
       "items": [
          {
            "answers": [
              {
                 "creation_date": 1538326991
              }
            ],
              "creation_date": 1538172095,
              "question_id": 52563147
           }
        ]
     }

现在,我想将"items"列表中的所有三个文件合并为一个文件,然后将其设为:

merged_json.json

   {
       "items": [
        {
         "answers": [
            {
              "creation_date": 1538172165
            },
            {
              "creation_date": 1538172205
            },
            {
              "creation_date": 1538172245
            }
          ],
            "creation_date": 1538172012,
            "question_id": 52563137
          },
          {
            "answers": [
             {
               "creation_date": 1538326991
             }
            ],
           "creation_date": 1538172095,
           "question_id": 52563147
          },
          {
           "answers": [
             {
               "creation_date": 1538180453
             }
            ],
            "creation_date": 1538172112,
            "question_id": 52563150
          },
          {
            "answers": [
              {
                 "creation_date": 1538326991
              }
            ],
            "creation_date": 1538172095,
            "question_id": 52563147
           }
        ]
     }

因此,像上面的"items"应该串联在一起。

我已经想出一个解决方案,但无法解决。 这是我到目前为止所得到的:

read_files = glob.glob("ExampleFile*.json")
output_list = []

for f in read_files:
    with open(f, "rb") as infile:
        output_list.append(json.load(infile))

all_items = []
for json_file in output_list:
    all_items += json_file['items']

textfile_merged = open('merged_json.json', 'w')
textfile_merged.write(str(all_items))
textfile_merged.close()

不幸的是,这给我留下了一个混乱的json文件,该文件只包含"items"内部的字典。

如何创建像merged_json.json这样的文件?

谢谢。

5 个答案:

答案 0 :(得分:0)

一种可行的方式,这将导致更清晰的代码来定义一个函数,该函数接受两个JSON对象并返回这两个对象的组合。

def merge (json_obj_1, json_obj_2):
    items = json_obj_1['items'] + json_obj_2['items']
    return { 'items': items }

然后,在您拥有output_list之后:

result = reduce(merge, output_list)

结果将是您要查找的对象。

如果您不熟悉reduce函数,请查看以下网页:

http://book.pythontips.com/en/latest/map_filter.html

它简要说明了reduce的用法以及map和filter。它们非常有用。

答案 1 :(得分:0)

read_files = glob.glob("ExampleFile*.json")                                                                                                                                                                         
output_list = []                                                                                                                                                                                                    

for f in read_files:                                                                                                                                                                                                
with open(f, "rb") as infile:                                                                                                                                                                                     
   output_list.append(json.load(infile))                                                                                                                                                                           

final_json = {}                                                                                                                                                                                                                                                                                                                                                                                             
all_items = []                                                                                                                                                                                                      
for json_file in output_list:                                                                                                                                                                                       
   all_items.extend(json_file['items'])                                                                                                                                                                              

final_json['items'] = all_items                                                                                                                                                                                     

textfile_merged = open('merged_json.json', 'w')                                                                                                                                                                     
textfile_merged.write(str(final_json)) 

答案 2 :(得分:0)

您正在使用json模块将JSON文件转换为Python对象,但没有使用该模块将这些Python对象 back 转换为JSON。最后不要这样

textfile_merged.write(str(all_items))

尝试一下:

json.dump({ "items": all_items }, textfile_merged)

(请注意,这还将all_items数组包装在字典中,以便您获得期望的输出,否则输出将是JSON数组,而不是带有"items"键的对象)

答案 3 :(得分:0)

我建议您使用json,它专门用于JSON对象操作。您可以执行以下操作:

    import json

with open('example1.json') as f:
    data1 = json.load(f)

with open('example2.json') as f:
    data2 = json.load(f)

with open('example3.json') as f:
    data3 = json.load(f)

items1 = data1["items"]
#print(json.dumps(items1, indent=2))
items2 = data2["items"]
items3 = data3["items"]

listitem = [items1, items2, items3]
finaljson = {"items" : []}

finaljson["items"].append(items1)
finaljson["items"].append(items2)
finaljson["items"].append(items3)
print(json.dumps(finaljson, indent=2))

with open('merged_json.json', "w") as f:
    f.write(json.dumps(finaljson, indent=2))

其中json.load()将字符串转换为json对象,而json.dumps()将json转换为字符串。参数indent可让您以扩展方式打印对象。

答案 4 :(得分:0)

如果只想按顺序合并所有json文件,

  1. 转到所有 json 文件所在的文件夹,全选并将第一个重命名为“yourchoice”,这样做将按顺序排列,即 yourchoice1,yourchoice2 ...

  2. 接下来进入 cmd 并输入:copy *.json "outputfilename".json

  3. 您的所有 json 文件都按顺序合并到“outputfilename”.json 文件中