-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_to_tinydb.py
More file actions
34 lines (27 loc) · 1.03 KB
/
json_to_tinydb.py
File metadata and controls
34 lines (27 loc) · 1.03 KB
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
import json
from tinydb import TinyDB
# Open the TinyDB database file named 'writers_toolkit.json'
db = TinyDB('writers_toolkit.json')
# Get references to the two tables: one for tools and one for global settings.
tools_table = db.table('tools')
settings_table = db.table('settings')
# Clear any existing data in the tables
tools_table.truncate()
settings_table.truncate()
# Read configuration from the file 'tools_config.json'
with open("tools_config.json", "r") as f:
config = json.load(f)
# Insert each top-level key appropriately.
for key, value in config.items():
if key == "_global_settings":
# Insert global settings into the settings table
settings_table.insert(value)
else:
# Insert tool configuration into the tools table.
# Ensure the document has a "name" key.
tool_doc = {"name": key}
tool_doc.update(value)
tools_table.insert(tool_doc)
print("Configuration has been successfully inserted into 'writers_toolkit.json'")
# Explicitly close the database
db.close()