Skip to main content

A lightweight local JSON database

Project description

JSONLite

Build Status PyPI License Issues PRs Welcome

JSONLite is a lightweight, local JSON database for simple data storage.

  • Like SQLite, it's a local database.
  • Its API is 100% modeled after MongoDB, making it easy to migrate between MongoDB and JSONLite.

Features

  • Zero dependency
  • Store JSON data locally
  • MongoDB-like API, Compatible with pymongo
  • Allows multiple processes to read/write concurrently

Table of Contents

Installation

pip install jsonlite

Data Layout in json file

{
    "data": [
        {   "_id": 1,
            "name": "Alice",
            "age": 30
        },
        {   "_id": 2,
            "name": "Bob",
            "age": 25
        },
        {   "_id": 3,
            "name": "Charlie",
            "age": 20
        }
    ]
}

Direct Usage

You can use JSONlite directly to perform CRUD operations.

>>> from jsonlite import JSONlite

>>> # Initialize the database
>>> db = JSONlite('mydatabase.json')

>>> # Inserting one document
>>> result = db.insert_one({"name": "John Doe", "age": 30})
>>> result.inserted_id
1

>>> # Inserting multiple documents
>>> result = db.insert_many([
...     {"name": "Jane Doe", "age": 25},
...     {"name": "Alice", "age": 28}
... ])
>>> result.inserted_ids
[2, 3]

>>> # Finding one document
>>> document = db.find_one({"name": "John Doe"})
>>> document
{'_id': 1, 'name': 'John Doe', 'age': 30}

>>> # Finding multiple documents
>>> documents = db.find({"age": {"$gte": 25}})
>>> documents
[
    {'_id': 1, 'name': 'John Doe', 'age': 30},
    {'_id': 2, 'name': 'Jane Doe', 'age': 25},
    {'_id': 3, 'name': 'Alice', 'age': 28}
]

>>> # Updating one document
>>> result = db.update_one({"name": "John Doe"}, {"$set": {"age": 31}})
>>> result.matched_count, result.modified_count
(1, 1)

>>> # Updating multiple documents
>>> result = db.update_many({"age": {"$gte": 25}}, {"$set": {"status": "active"}})
>>> result.matched_count, result.modified_count
(3, 3)

>>> # Deleting one document
>>> result = db.delete_one({"name": "John Doe"})
>>> result.deleted_count
1

>>> # Deleting multiple documents
>>> result = db.delete_many({"age": {"$lt": 30}})
>>> result.deleted_count
2

Patching pymongo to use JSONlite

Alternatively, you can patch pymongo to use JSONlite and interact with JSON files as if you were using MongoDB. This allows you to use the familiar pymongo API with JSON data.

>>> from jsonlite import pymongo_patch

>>> pymongo_patch()

>>> from pymongo import MongoClient

>>> client = MongoClient('jsonlite://database')
>>> db = client.test_database
>>> collection = db.test_collection
>>> insert_result = collection.insert_one({"name": "Alice", "age": 30})
>>> # Just like using pymongo
>>> collection.drop()

License

JSONLite is licensed under the MIT License. See the LICENSE file for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonlite-0.0.9.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

jsonlite-0.0.9-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file jsonlite-0.0.9.tar.gz.

File metadata

  • Download URL: jsonlite-0.0.9.tar.gz
  • Upload date:
  • Size: 18.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for jsonlite-0.0.9.tar.gz
Algorithm Hash digest
SHA256 1ad37eac32dbc414be8f94f19436db82a84dfca36b6ce12ea48a802e7a6e9e08
MD5 6af73290b2f22cff8df29083c43acfb8
BLAKE2b-256 c1fc133ff8eeec60629241b62c8a2d12ef1ea6b7915ea8a62a2ca3899aa93ecf

See more details on using hashes here.

File details

Details for the file jsonlite-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: jsonlite-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for jsonlite-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 1ec04da9fea375dc2d51de0a8fbc24a0ab9cac3f20c8db0ec21c42cd88781c8a
MD5 15dfbcc2358fbfbcc24f2fc8eed8c350
BLAKE2b-256 b98092b81e5555c60c9dee04701de228caf9654811a0d1c26b4227a2c1a8653a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page