Skip to main content

JsonDB Logo

JsonDB-Python

PyPI Version License Documentation

A simple, lightweight, and file-based JSON database library for Python, now with proper error handling, a modular structure, and a powerful interactive REPL.

Overview

jsondb-python provides two ways to manage your data:

  1. As a Python Library: A simple interface for storing and retrieving data in a JSON file. It's perfect for small projects where a full-fledged database is overkill.
  2. As an Interactive REPL: A feature-rich command-line interface (CLI) to manage your databases directly from the terminal, no scripting required.

Installation

pip install jsondb-python

For the best REPL experience with auto-completion and history, it is recommended to install prompt_toolkit:

pip install prompt-toolkit

Usage

As a Python Library (Quick Start)

from jsondb import JsonDB

# Initialize the database
db = JsonDB('my_database.json')

try:
    # Create a table
    db.create_table('users')

    # Insert data
    db.insert_data('users', {'id': 1, 'name': 'Alice', 'role': 'admin'})
    db.insert_data('users', {'id': 2, 'name': 'Bob', 'role': 'user'})

    # Update data where role is 'user'
    db.update_data(
        'users',
        condition=lambda user: user.get('role') == 'user',
        new_data={'id': 2, 'name': 'Bob', 'role': 'member'}
    )

    # Delete data where id is 1
    db.delete_data('users', condition=lambda user: user.get('id') == 1)

    # Show final data
    db.show_data('users')

except db.TableExistsError as e:
    print(f"Setup failed because a table already exists: {e}")
except db.Error as e: # Catch any library-specific error
    print(f"An error occurred with the database: {e}")
except Exception as e:
    print(f"A general error occurred: {e}")

Interactive REPL (CLI)

The library includes a powerful interactive REPL (Read-Eval-Print Loop) to manage your databases from the command line.

Launching the REPL:

# Start the REPL in the main menu
jsondb

# Or open a database file directly
jsondb ./path/to/your/database.json

Key Features:

  • Interactive Management: Create, edit, and manage your JSON databases without writing Python code.
  • Smart Auto-Completion: Press Tab to get suggestions for commands, file paths, and table names.
  • Command History: Use the Up/Down arrow keys to navigate your command history.
  • User-Friendly Interface: A colorized and structured interface makes database management easy.
  • Built-in Help: Type .help in any mode to see a list of available commands.
  • Safe Operations: Features auto-saving on exit and automatic backup creation to prevent data loss.

Example Session:

$ jsondb
🌟 JsonDB >>> .build
📁 Enter database path (example: data/mydb.json): users.json
✅ Database 'users.json' created/opened successfully!
💡 Use '.create <table_name>' to create a new table
📦 [users.json] >>> .create people
✅ Table 'people' created successfully!
📦 [users.json] >>> .use people
✅ Successfully selected table 'people'!
💡 Use '.insert' to add data
📋 [people] >>> .insert
# ... interactive prompt to add data ...
✅ Data added successfully!
📋 [people] >>> .show
# ... displays table data ...
📋 [people] >>> .exit
💾 Performing auto-save before exiting...
👋 Thank you for using JsonDB REPL!
💾 Your data has been safely saved

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for jsondb-python 1.0.9

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

Source distribution (sdist)

Source distribution for jsondb-python 1.0.9
File Size Uploaded
jsondb_python-1.0.9.tar.gz 21.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsondb-python 1.0.9
File Interpreter ABI Platform
jsondb_python-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 42.0 kB

Release files / jsondb_python-1.0.9.tar.gz

Download URL jsondb_python-1.0.9.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
cc8ebdc78b6351ead69cc0356f02214c1fe2d9195e70bcf1b4f11bdcea87b5ca
BLAKE2b-256 checksum
How to use checksums
24c96f972101357eb83eb0abc8010f37ff3c41819fa0b5df91a2e1a36b350473
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / jsondb_python-1.0.9-py3-none-any.whl

Download URL jsondb_python-1.0.9-py3-none-any.whl
Size 20.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06ca52e13746c5e3626a00bc9992d2b84b325533f6d5f967c249d6dcfeca16d7
BLAKE2b-256 checksum
How to use checksums
27442a987664edaf126463e92e0c98cb15a55dc5395dc31d7db7a6ab0e6a27bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

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