Package: py3jsondb
Py3jsondbPython dict based database with persistence and search capabilitiesFor those times when you need something simple and sql is overkillsupport add, update, delete in any level nested json treeFeaturespure pythonsave and load from filesearch recursively by key and key/value pairsfuzzy searchsupports arbitrary objectssupports comments in saved filessupports operation in any level nested json treeInstallpip3installpy3jsondbUsageJson Format{'table1 name':[{'entry1_item1_name':'entry1_item1_value,'entry1_item2_name':'entry1_item2_value,....},{'entry2_item1_name':'entry2_item1_value','entry2_item2_name':'entry2_item2_value',....},.........]'table2 name':[........]}JsonDatabaseEver wanted to search a dict?Let's create a dummy database with users
and Add some entrys of usersfrompy3jsondbimportJsonDatabasedb_path="users.db"withJsonDatabase("users",db_path)asdb:# add some users to the databaseforuserin[{"name":"bob","age":12},{"name":"bobby"},{"name":["joe","jony"]},{"name":"john"},{"name":"jones","age":35},{"name":"joey","birthday":"may 12"}]:db.add_entry(user)# pretty print database contentsdb.print()# auto saved when used with context manager# db.save()add, change,delete tablefrompy3jsondbimportJsonDatabasedb_path="users.db"withJsonDatabase("users",db_path)asdb:db.add_table("test")# add a new table, now you have two tables: users and test# and change the current table to test.db.use_table("users")# change the current table to users.db.delete_table("test")#delete the table test, now have only one table:users.updating an existing entry and remove entry# get database itemitem={"name":"bobby"}item_id=db.get_entry_id(item)ifitem_id>=0:new_item={"name":"don't call me bobby"}db.update_entry(item_id,new_item)else:print("item not found in database")db.remove_entry(item_id)# clear changes since last commitdb.reload()add, update and delete child in entrysfrompy3jsondbimportJsonDatabasedb_path="users.db"withJsonDatabase("users",db_path)asdb:db.add_entry({"name":"john","age":"33"})# add a new entryentry_id=db.get_entry_id({"name":"john"},strictly=False)# get the entry iddb.add_child_to_entry(entry_id,{"name":"bobby","age":12})# add child to entrydb.update_child_of_entry(entry_id,{"name":"bobby","age":15})# update child of entry to new valuedb.delete_child_of_entry(entry_id)# delete child of entryadd, update and delete any node  in any nested json treefrompy3jsondbimportJsonDatabasedb_path="users.db"withJsonDatabase("users",db_path)asdb:entry_id=db.add_entry({"name":"john","age":"33"})# add a new entrydb.add_child_to_entry(entry_id,{"name":"bobby","age":12})# add child to entrychild_path=db.get_child_path_of_entry(entry_id)# get the path of entry#child_path = db.get_path_by_key_value("name","bobby")[0]# get the path via name and bobbydb.add_child_to_path(child_path,{"grade":100})# add child to the pathdb.delete_child_of_path(child_path)# delete child of pathsearch entries by keyfrompy3jsondbimportJsonDatabasedb_path="users.db"db=JsonDatabase("users",db_path)# load db created in previous example# search by exact key match, return a list of resultusers_with_defined_age=db.search_by_key("age")foruserinusers_with_defined_age:print(user["name"],user["age"])# fuzzy searchusers=db.search_by_key("birth",fuzzy=True)foruser,confinusers:print("matched with confidence",conf)print(user["name"],user["birthday"])search by key value pair# search by key/value pairusers_12years_old=db.search_by_value("age",12)foruserinusers_12years_old:assertuser["age"]==12# fuzzy searchjon_users=db.search_by_value("name","jon",fuzzy=True)foruser,confinjon_users:print(user["name"])print("matched with confidence",conf)# NOTE that one of the users has a list instead of a string in the name, it also matchesYou can save arbitrary objects to the databasefrompy3jsondbimportJsonDatabasedb=JsonDatabase("users","~/databases/users.json")classUser:def__init__(self,email,key=None,data=None):self.email=emailself.secret_key=keyself.data=datauser1=User("first@mail.net",data={"name":"jonas","birthday":"12 May"})user2=User("second@mail.net","secret",data={"name":["joe","jony"],"age":12})# objects will be "jsonified" here, they will no longer be User objects# if you need them to be a specific class use some ORM lib instead (SQLAlchemy is great)db.add_entry(user1)db.add_entry(user2)# search entries with non empty keyprint(db.search_by_key("secret_key"))# search in user provided dataprint(db.search_by_key("birth",fuzzy=True))# search entries with a certain valueprint(db.search_by_value("age",12))print(db.search_by_value("name","jon",fuzzy=True))