Question:
I use Replit’s database system and I think it it great, however i am struggling to create a JSON file with the database saved in it. I want this for a backup save just in case anything goes wrong with my code.
Is there a way to do this?
Repl link:
from replit import db
import json
with open("data.json", "w") as f:
json.dump(db, f)# this says that db isnt an object of json serializable
import json
import os
import replit
keys = replit.db.keys()
data = {}
for key in keys:
data[key] = replit.db[key]
file_path = os.path.join(os.getcwd(), "database_backup.json")
with open(file_path, "w") as f:
json.dump(data, f, indent=2)
Btw i didnt test this lolz
yeah should work ig
@thajan2009
Please mark this as a solution if this works, lmk if you have any errors il respond asap
if this doesn’t work i swear im gonna
2 Likes
Welcome to the community @thajan2009 !
As you have found, replit db ins’t serializable but you can serialize the keys and their corresponding values individually.
The code @Idkwhttph provided can get the job done!
1 Like
Yes and I am patiently ( not ) waiting for my first solution right here.
1 Like
idk had it almost right… this works:
from replit import db
import json
db["mykey"] = "value"
new_json = {}
for key in db.keys():
new_json[key] = db[key]
with open("output.json", "w") as file:
file.write(json.dumps(new_json))
3 Likes
the 1 issue. Can that handle recursive databases?
1 Like
What do you mean by recursive databases? Keys that hold dictionaries? …
1 Like
And arrays, but yes, that is what I mean.
It should work just fine. All keys content should be json serializable
Is “ObservedDict” json serializable? I think the lib has a special serializer for the special dicts.
dragonhunter1:
ObservedDict
If not, just call .value
on it.
1 Like
Ah, I didn’t realize replit made everything observed. This should fix that:
from replit import db
import replit
db["key"] = "value"
db["json"] = {"key" : "value"}
def to_json(_dict):
output = {}
for key in _dict.keys():
if type(_dict[key]) == replit.database.database.ObservedDict:
output[key] = to_json(_dict[key])
else:
output[key] = _dict[key]
return output
print(to_json(db))
2 Likes
I tried that, it didn’t work
I’m guessing because of nested things?
On a different note, why not have:
be:
if type(_dict[key]) == ObservedDict:
That would mean adding this import though:
from replit.database.database import ObservedDict
yeah I just didn’t want to import it from that cause I was too lazy
1 Like
Understandable, have a nice day.
Thanks a lot, this worked for the most part but im not sure how to deal with ObservedList but this works.
def to_json(_dict):
output = {}
for key in _dict.keys():
if type(_dict[key]) == replit.database.database.ObservedDict:
output[key] = to_json(_dict[key])
else:
output[key] = str(_dict[key])
return output
with open("database_backup.json", "w") as file:
json.dump(to_json(db), file, indent=2)
1 Like
I know this has already been solved, but there is an easier way to do this without any code.
There is a workspace extension you can install called ReplDB Importer and Exporter which allows you to export your DB to JSON.
system
Closed
June 11, 2023, 3:43pm
21
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.