简体   繁体   中英

How to open multiple JSON file from folder and merge them in single JSON file in python?

Suppose there are 3 files - data1.json, data2.json, data3.json.

Let's say data1.json contains -

{ 
   "Players":[ 
      { 
         "name":"Alexis Sanchez",
         "club":"Manchester United"
      },
      { 
         "name":"Robin van Persie",
         "club":"Feyenoord"
      }
   ]
}

data2.json contains -

{ 
   "Players":[ 
      { 
         "name":"Nicolas Pepe",
         "club":"Arsenal"
      }
   ]
}

data3.json contains -

{ 
   "players":[ 
      { 
         "name":"Gonzalo Higuain",
         "club":"Napoli"
      },
      { 
         "name":"Sunil Chettri",
         "club":"Bengaluru FC"
      }
   ]
}

A merge of these 3 files will generate a file with the following data. result.json -

{ 
   "players":[ 
      { 
         "name":"Alexis Sanchez",
         "club":"Manchester United"
      },
      { 
         "name":"Robin van Persie",
         "club":"Feyenoord"
      },
      { 
         "name":"Nicolas Pepe",
         "club":"Arsenal"
      },
      { 
         "name":"Gonzalo Higuain",
         "club":"Napoli"
      },
      { 
         "name":"Sunil Chettri",
         "club":"Bengaluru FC"
      }
   ]
}

How to open multiple JSON file from folder and merge them in single JSON file in python?

My Approach:

import os, json
import pandas as pd
path_to_json =  #path for all the files.
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

jsons_data = pd.DataFrame(columns=['name', 'club'])

for index, js in enumerate(json_files):
    with open(os.path.join(path_to_json, js)) as json_file:
        json_text = json.load(json_file)

        name = json_text['strikers'][0]['name']
        club = json_text['strikers'][0]['club']

        jsons_data.loc[index] = [name, club]

print(jsons_data)

This could to the job for you:

import json
import glob
import pprint as pp #Pretty printer

combined = []
for json_file in glob.glob("*.json"): #Assuming that your json files and .py file in the same directory
    with open(json_file, "rb") as infile:
        combined.append(json.load(infile))



pp.pprint(combined)

This does exactly what you wanted,

import json, glob

merged_json = []
for json_file in glob.glob("*json"):
    with open(json_file, "rb") as file:
      json_data = json.load(file)
      if "Players" in json_data:
        merged_json += json_data["Players"]
      else:
        merged_json += json_data["players"]

to_json = json.dumps(merged_json)
print (to_json)

Output

[{"name": "Alexis Sanchez", "club": "Manchester United"}, {"name": "Robin van Persie", "club": "Feyenoord"}, {"name": "Nicolas Pepe", "club": "Arsenal"}, {"name": "Gonzalo Higuain", "club": "Napoli"}, {"name": "Sunil Chettri", "club": "Bengaluru FC"}]

Both of the answers above seems like they work. Can someone explain why use the "binary" mode to read the files instead of just reading them?

with open(json_file, "rb") as infile:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM