Skip to content

Commit

Permalink
Added json encode
Browse files Browse the repository at this point in the history
  • Loading branch information
johc committed Mar 27, 2024
1 parent a243b2f commit 275a6cb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions fetch_data.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from pymongo import MongoClient
import json
import os
from bson import ObjectId

# JSON Encoder for handling MongoDB ObjectId
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)

# Connect to MongoDB
client = MongoClient(os.environ['MONGODB_URI'])
db = client.FMD # Assuming 'FMD' is your database name
db = client.FMD # Replace 'FMD' with your actual database name

# Function to fetch and save collection data
def fetch_and_save(collection_name, file_path):
data = list(db[collection_name].find({}))
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as file:
json.dump(data, file)
file.write(JSONEncoder().encode(data))

# Fetch and save FMD.Endpoints
fetch_and_save("Endpoints", 'fmd-telemetry/fmd-telemetry/src/assets/Endpoints.json')
Expand Down

0 comments on commit 275a6cb

Please sign in to comment.