Skip to main content

Py3jsondb

Python dict based database with persistence and search capabilities

For those times when you need something simple and sql is overkill

support add, update, delete in any level nested json tree

Features

  • pure python
  • save and load from file
  • search recursively by key and key/value pairs
  • fuzzy search
  • supports arbitrary objects
  • supports comments in saved files
  • supports operation in any level nested json tree

Install

pip3 install py3jsondb

Usage

Json 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':[
                    ........
                    ]
}

JsonDatabase

Ever wanted to search a dict?

Let's create a dummy database with users and Add some entrys of users

from py3jsondb import JsonDatabase

db_path = "users.db"

with JsonDatabase("users", db_path) as db:
    # add some users to the database

    for user in [
        {"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 contents
    db.print()


# auto saved when used with context manager
# db.save()

add, change,delete table

from py3jsondb import JsonDatabase

db_path = "users.db"

with JsonDatabase("users", db_path) as db:
    
    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 item
item = {"name": "bobby"}

item_id = db.get_entry_id(item)

if item_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 commit
db.reload()

add, update and delete child in entrys

from py3jsondb import JsonDatabase

db_path = "users.db"

with JsonDatabase("users", db_path) as db:
    
    db.add_entry({"name":"john","age":"33"})
    # add a new entry
    entry_id = db.get_entry_id({"name":"john"},strictly=False)
    # get the entry id
    db.add_child_to_entry(entry_id,{"name":"bobby","age":12})
    # add child to entry
    db.update_child_of_entry(entry_id,{"name":"bobby","age":15})
    # update child of entry to new value
    db.delete_child_of_entry(entry_id)
    # delete child of entry

add, update and delete any node in any nested json tree

from py3jsondb import JsonDatabase

db_path = "users.db"

with JsonDatabase("users", db_path) as db:
    
    entry_id = db.add_entry({"name":"john","age":"33"})
    # add a new entry
    db.add_child_to_entry(entry_id,{"name":"bobby","age":12})
    # add child to entry
    child_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 bobby
    db.add_child_to_path(child_path,{"grade":100})
    # add child to the path
    db.delete_child_of_path(child_path)
    # delete child of path

search entries by key

from py3jsondb import JsonDatabase

db_path = "users.db"

db = JsonDatabase("users", db_path) # load db created in previous example

# search by exact key match, return a list of result
users_with_defined_age = db.search_by_key("age")

for user in users_with_defined_age:
    print(user["name"], user["age"])
    
# fuzzy search
users = db.search_by_key("birth", fuzzy=True)
for user, conf in users:
    print("matched with confidence", conf)
    print(user["name"], user["birthday"])

search by key value pair

# search by key/value pair
users_12years_old = db.search_by_value("age", 12)

for user in users_12years_old:
    assert user["age"] == 12

# fuzzy search
jon_users = db.search_by_value("name", "jon", fuzzy=True)
for user, conf in jon_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 matches

You can save arbitrary objects to the database

from py3jsondb import JsonDatabase

db = JsonDatabase("users", "~/databases/users.json")


class User:
    def __init__(self, email, key=None, data=None):
        self.email = email
        self.secret_key = key
        self.data = data

user1 = 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 key
print(db.search_by_key("secret_key"))

# search in user provided data
print(db.search_by_key("birth", fuzzy=True))

# search entries with a certain value
print(db.search_by_value("age", 12))
print(db.search_by_value("name", "jon", fuzzy=True))

Release files for py3jsondb 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py3jsondb 1.0.0
File Size Uploaded
py3jsondb-1.0.0.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py3jsondb 1.0.0
File Interpreter ABI Platform
py3jsondb-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.3 kB

Release files / py3jsondb-1.0.0.tar.gz

Download URL py3jsondb-1.0.0.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2afd2b53de8682b158da993ebf75c0b20c8d2909e338b946e057a00cffc0023e
BLAKE2b-256 checksum
How to use checksums
bc5bb290df1c4b1988ad8c549af480da0bf9dd89d3c7e99c4d1ca50facd3fd6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / py3jsondb-1.0.0-py3-none-any.whl

Download URL py3jsondb-1.0.0-py3-none-any.whl
Size 14.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4ffd380eb26b3aa61ea4becbc8afe52142baf08179bc180dc56ca2ea9abb16c
BLAKE2b-256 checksum
How to use checksums
78d1e929ed91cb64729a5bfb28da814e7ab4815c7e38d4009cceee52cb403921
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page