-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadJsonDatatoDB.py
51 lines (40 loc) · 1.48 KB
/
LoadJsonDatatoDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pymongo.mongo_client import MongoClient
import os
import json
import traceback
from dotenv import load_dotenv
load_dotenv()
try:
# Connect to MongoDB
uri = os.getenv("MONGODB_ATLAS_URI")
client = MongoClient(uri, serverSelectionTimeoutMS=5000)
db = client["ZomatoData"]
collection = db["CompleteRestaurantData"]
print("Connected to MongoDB")
# Read and insert data from JSON files
path = "C:/Users/raisi/Downloads/archive"
for i in range(1, 6):
jsonData = f"{path}/file{i}.json"
if not os.path.exists(jsonData):
print(f"File {jsonData} not found")
continue
with open(jsonData) as f:
data = json.load(f)
# Ensure data is a list
if not isinstance(data, list):
print(f"Warning: {jsonData} does not contain a list at the top level. Skipping.")
continue
total_inserted = 0
for item in data:
records = item.get("restaurants") # Use `.get()` to prevent KeyError
if not records: # If 'restaurants' is missing or empty
print(f"Warning: No valid records found in {jsonData}. Skipping.")
continue
collection.insert_many(records)
total_inserted += len(records)
print(f"Inserted {total_inserted} records from {jsonData}")
print("Data loaded successfully to MongoDB")
client.close()
except Exception as e:
print("Error:", e)
traceback.print_exc()