localdb-json
raw JSON → 1.0 verified Fri May 01 auth: no python
A helper script for easily handling JSON files as a local database. Provides a simple class to load, manipulate, and save data persisted in a JSON file. Intended for small-scale local storage needs. Version 1.0, no active development observed.
pip install localdb-json Common errors
error SyntaxError: invalid syntax when using `import localdb-json` ↓
cause Using a hyphen in import statement instead of underscore.
fix
Replace hyphen with underscore:
from localdb_json import JSONDatabase error KeyError: 'users' after appending data ↓
cause Calling db['users'] before saving when initializing a new database without setting that key.
fix
Initialize the database with a structure: db = JSONDatabase('data.json', default={'users': []}) or set key before accessing.
Warnings
gotcha The library uses a hyphen in the package name (localdb-json). When importing in Python, you must replace hyphens with underscores: `from localdb_json import JSONDatabase`. Attempting `import localdb-json` will raise a syntax error. ↓
fix Use underscore in import: from localdb_json import JSONDatabase
gotcha JSONDatabase saves data back to the file only when .save() is called explicitly. Modifying the in-memory dict does not persist automatically. ↓
fix Always call db.save() after making changes to write to disk.
gotcha The library does not handle concurrent access. Writing from multiple processes or threads may cause corruption or data loss. ↓
fix Use only in single-threaded, single-process scenarios, or implement your own locking mechanism.
Imports
- JSONDatabase wrong
import localdb-json from localdb-json import JSONDatabasecorrectfrom localdb_json import JSONDatabase
Quickstart
from localdb_json import JSONDatabase
db = JSONDatabase('data.json')
db['users'] = [{'name': 'Alice', 'age': 30}]
db.save()
print(db['users'])